Dot Net or Javascript problem???

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a problem and I'm not sure whether it's Dot Net or Javascript so I'm
posting here.

I have a web page with a text field and a custom validator. The validator
points to a javascript function. The idea is to prevent saving of entries
that are less than 6 characters. The function works fine as long as there is
at least 1 character in the field. What I cannot understand is why it does
not fire at all if the textbox is blank. Here's the code. I put in an alert
to check if it was being called. (Perhaps I should mention that I am an
absolute beginner in Javascript.)

function ValidateLicense(sender,args)
{
var r = document.getElementById("<%= driversLicense.ClientID %>");
args.IsValid = true;
// window.alert(r.value.length);
if (r.value.length < 6)
{
args.IsValid = false;
}
}
 
Those don't fire on blank fields. You have to use a requiredfieldvalidator
in conjunction with your custom one.

The technical reason I am guesing is that the function is hooked up to fire
on the lose focus event, which wouldn't fire if the user never set focus to
the text box in the first place.
 
Allow me to refine Marina's answer.

You can omit the control ID assigned to the ControlToValidate property. The
CustomValidator will not know the textbox is blank and will always run.
However, it will not fire during the onchange event.

I put together an article on various problems users face with validators:
http://aspalliance.com/699.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 
Thanks! (I clearly have some studying to do...)

Peter Blum said:
Allow me to refine Marina's answer.

You can omit the control ID assigned to the ControlToValidate property. The
CustomValidator will not know the textbox is blank and will always run.
However, it will not fire during the onchange event.

I put together an article on various problems users face with validators:
http://aspalliance.com/699.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 
Back
Top