Dot Net or Javascript problem???

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;
}
}
 
M

Marina

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.
 
P

Peter Blum

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
 
G

Guest

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
 

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,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top