|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Dot Net or Javascript problem???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. Show quoteHide quote "B. Chernick" <BChern***@discussions.microsoft.com> wrote in message news:3FDB9EA6-2445-43E4-94F3-A76B42EEA634@microsoft.com... >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; > } > } 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: PLB***@PeterBlum.com Creator of "Professional Validation And More" at http://www.peterblum.com/vam/home.aspx Show quoteHide quote "B. Chernick" <BChern***@discussions.microsoft.com> wrote in message news:3FDB9EA6-2445-43E4-94F3-A76B42EEA634@microsoft.com... >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; > } > } Thanks! (I clearly have some studying to do...)
Show quoteHide quote "Peter Blum" wrote: > 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: PLB***@PeterBlum.com > Creator of "Professional Validation And More" at > http://www.peterblum.com/vam/home.aspx > > "B. Chernick" <BChern***@discussions.microsoft.com> wrote in message > news:3FDB9EA6-2445-43E4-94F3-A76B42EEA634@microsoft.com... > >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; > > } > > } > > >
Other interesting topics
global functions?
MSWord Common functionality across pages, what's the best solution? reference to a "global" variable from within an aspx page Asp.net 2.0 caching and images DataGrid, Edit and the Enter Key ASP.NET Embedded Resources Very Strange Problem! (Derived Controls) - Help! Architectural assistance needed! Help on LinkButton vs PushButton ?? |
|||||||||||||||||||||||