Custom Validation Cntrl

  • Thread starter Thread starter Gary Larimer
  • Start date Start date
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.
 
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
 
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)
 
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)
 
Thank you for the replies.

Changing args.value to args.Value solved the problem.
 
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)
 
Back
Top