Need help on validation on a group of controls

L

Learner

Hi Friends,
I have two things that I am running short of ideas on how to solve.
I am working on medium sized search form where in I will have to do
considerable validations befor it submits the form.

1).I have to put a validation on a text box ( _txtVIN ) such that it
min and max charecters should be 8. In other words user shouldn't be
able to enter more than 8 charecters of the total VIN numner. At the
same user must enter 8 charecters if at all he eneters some thing in
the text field. I have put the max length property to put a cap on the
number of charectors entered. But no clue how do I do it in the case
min of 8 charecters?

2). I have 3 text boxes. I need to make sure that atleast one filed has
the text in it before it submits the form. In other words user must
enter atleast a value in any of the text boxes. I am not sure what
control would do best to do this?

say _txtYear, _txtMake, _txtModel and the form shoudln't submit unless
user provides atleast a value in the respective fileds.


Any inputs is greatly appreciated,
Thanks,
-L
 
N

Nathan Sokalski

My suggestion for both of these problems would be to use the CustomValidator
control. For your first problem, I would use the following as your
server-side event and the CustomValidator's ClientValidationFunction
property:

Sub val8Chars_ServerValidate(source As Object, args As
ServerValidateEventArgs) Handles val8Chars.ServerValidate
If txtMyTextBox.Text.Length = 8 Then args.IsValid = True Else args.IsValid
= False
End Sub

<script type="text/javascript">
function val8Chars_ServerValidate(source,args)
{
if(args.value.length==8)
args.IsValid=true;
else
args.IsValid=false;
}
</script>


For your second problem, I would use the following server-side event:

Sub valMustPickOne_ServerValidate(source As Object, args As
ServerValidateEventArgs) Handles valMustPickOne.ServerValidate
If txtMyTextBox1.Text = "" AndAlso txtMyTextBox2.Text = "" AndAlso
txtMyTextBox3.Text = "" Then args.IsValid = False Else args.IsValid = True
End Sub

If you wanted to make a ClientValidationFunction function for this, I would
suggest generating it dynamically using the ClientID property of the three
controls and adding the script using the RegisterClientScriptBlock method
because the validation involves the value of multiple controls. for more
information on how to do this, see the ASP.NET documentation and JavaScript
documentation. Good Luck!
 
L

Learner

Hello Nathan,
I appriciate your response on this and the same time I am admit that
its been delayed in responding to this posting as I have been occupied
with other stuff. I am sorry about that.

Now, the server side event handling worked good for my _txtVIN text
box. here is the code

**************************************************************************
Protected Sub _CustvldVIN_ServerValidate(ByVal source As Object, ByVal
args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles
_CustvldVIN.ServerValidate
If _txtVIN.Text.Length = 8 Then
args.IsValid = True
Else
args.IsValid = False
_CustvldVIN.Text = "Please enter CUST valid VIN"
End If
End Sub
*************************************************************************
and I put the
If page.isValid then
End if
code in the _btn_search_click event and it returns false if length is
less than 8.


But when I try doing it using Javascript it validates the txt entere
the _txtVIN text box but it generates a java script error on the left
bottom of the browser
it says "Object expected" when I double clicked on the error. I am not
sure if this some thing to do with Internet explorer IE 7.0 (beta
version) I am using.

Here is the defenition of the CustomValidator control in my .aspx page
**********************************************************************************************
<asp:CustomValidator ID="_CustvldVIN" runat="server"
ControlToValidate="_txtVIN"
ErrorMessage="Please enter Cusvld VIN!"


OnServerValidate="_CustvldVIN_ServerValidate"
ClientValidationFunction="TestVIN">
</asp:CustomValidator>
**********************************************************************************************

and the java script function that I have in my common.js script file is


****************************************************************************************************
function TestVin(source,args)
{
if(args.value.length==8)
args.IsValid=true;
else
args.IsValid=false;
}
****************************************************************************************************


Am I missing some thing here?

Please advice,
Thanks
-L
 
N

Nathan Sokalski

I'm not sure what is wrong. It could have something to do with IE7, I
haven't tried the Beta version. One other thing that could be the problem
(different browsers are pickier than others, and who knows what IE7 wants)
is the JavaScript syntax. Try changing the JavaScript syntax to the
following and see if it makes a difference:

function TestVin(source,args)
{
if(args.value.length==8)
{
args.IsValid=true;
}
else
{
args.IsValid=false;
}
}

Even though this is the same algorithm, maybe IE7 requires the {} around the
code inside the if/else statement. Something else that you could try is
writing a small little html page to test the JavaScript function to see if
the function is really the problem. I will also admit that, even though I
have used them once or twice and am working on learning more about them, I
do not have the huge amount of experience with client-side validation being
used with the ASP.NET Validators, but as always, we come here to help each
other with what we know, and possibly learn more in the process, so I'll do
what I can! Good Luck!
 
L

Learner

Hello Nathan,
Thanks buddy. Yes you are right its all about playing with the new
technolog and its always fun. Thanks a bunch for your detailed
explanation. I will let you know if I could figure out and fix the bug
:)

Thanks buddy
-L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top