Custom Validation Cntrl

G

Gary Larimer

Created a Custom Validation Ctrl in EW2 to validate a text box entry. Want
to make sure entry is evenly divisible by 7, and that value is not 0.
However, args.IsValid = false, even if 77 is entered in text box. Tag and
script that were added to page follow:

<asp:CustomValidator id="CustomValidator1" runat="server"
ClientValidationFunction="ClientValidateSerial3"
ControlToValidate="SerialNumber3" Display="Dynamic" ErrorMessage="Entry
Invalid"></asp:CustomValidator>

<script type="text/javascript">

function ClientValidateSerial3(source, args) {

if(args.value == 0) {

args.IsValid = false;
return;
}

if(args.value % 7 == 0) {

args.IsValid = true;
}
else {

args.IsValid = false;
}
}

</script>

This is my first attemp at using a Custom Validator and using javascript.

Serverside code for validation was not present when I tested the above
script in EW2.

Also posted question on EW discussion group.

Thanks for any tips/information.
 
S

Scott M.

Why are you checking args.value?

If the value that needs to be validated is in a textbox, then that is what
you should be checking. This code should do what you're looking for:

<script type="text/javascript">

function ClientValidateSerial3(source, args) {

args.IsValid = false;

if (Textbox1.value % 7 == 0)
{
args.IsValid = true;
}
</script>

Also, don't forget to write the same logic, but in the ServerValidate event
for the customValidator control.

-Scott
 
B

bruce barker

you almost had it. the original javascript code used with webforms
breaks with convention and starts properties with a capital letter, so
its "args.Value".

-- bruce (sqlwork.com)
 
B

bruce barker

this is a bad practice. your javascript should be checking args.Value,
not looking up the control by name otherwise the validator is not
reusable. if you need other control properties, the source is the
validator control from which you access the actual control with
"source.controltovalidate"

-- bruce (sqlwork.com)
 
G

Gary Larimer

Thank you for the replies.

Changing args.value to args.Value solved the problem.
 
S

Scott M.

I disagree. As a customvalidator, its use would not necessarially need to
be written as reusable.


bruce barker said:
this is a bad practice. your javascript should be checking args.Value, not
looking up the control by name otherwise the validator is not reusable. if
you need other control properties, the source is the validator control
from which you access the actual control with "source.controltovalidate"

-- bruce (sqlwork.com)
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top