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

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

S. Justin Gengo

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
 
K

Ken Cox [Microsoft MVP]

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.
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top