JS validation doesnt work when NET build-in validators are used

  • Thread starter Thread starter Sean
  • Start date Start date
S

Sean

I am frustrated by the issue, and looking for a workaround.

I have a dropdown list, and a button and some other controls on the page.
When the button is clicked, I need to ensure a valid dropdown list item is
selected, (e.g. selectedIndex > 0, as the 1st item in ddl is dummy record).
Also, I need use some NET build-in validators for other controls.

What I did is:

- Create a JS function
function submitForm() {
if ( (document.getElementById("ddl").selectedIndex <= 0)) {
alert("nothing selected in ddl");
return false;
}

if (typeof(Page_ClientValidate) == 'function')
Page_ClientValidate();

if (Page_IsValid){
ValidatorOnSubmit();//this will fire postback
}
}

- on the server side add attributes to button.
btnSubmit.Attributes.add("onClick","submitForm()")


I wonder if anyone can help me out. Thanks.
 
Sean,

Use the required field validator instead of the javascript you wrote. The
required field validator has a property, "InitialValue", set that property
equal to the initial text in your dropdown and that will solve your problem.

Sincerely,

--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
 
There's a detailed discussion by Mike Moore on this issue:

http://www.aspalliance.com/kenc/faq6.aspx


I am frustrated by the issue, and looking for a workaround.

I have a dropdown list, and a button and some other controls on the page.
When the button is clicked, I need to ensure a valid dropdown list item is
selected, (e.g. selectedIndex > 0, as the 1st item in ddl is dummy record).
Also, I need use some NET build-in validators for other controls.

What I did is:

- Create a JS function
function submitForm() {
if ( (document.getElementById("ddl").selectedIndex <= 0)) {
alert("nothing selected in ddl");
return false;
}

if (typeof(Page_ClientValidate) == 'function')
Page_ClientValidate();

if (Page_IsValid){
ValidatorOnSubmit();//this will fire postback
}
}

- on the server side add attributes to button.
btnSubmit.Attributes.add("onClick","submitForm()")


I wonder if anyone can help me out. Thanks.
 
Back
Top