javascript form flush fields - problem

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

javascript form flush fields - problem

Post by dream2rule »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


[syntax="javascript"]
<script language="JavaScript">
function validateForm()
{
// check name
if (document.forms[0].elements[0].value == "")
{
alert ("Please enter a name!");
return false;
}
// check password length
if (document.forms[0].elements[1].value.length < 6)
{
alert ("Please enter a password 
of at least 6 characters!");
return false;
}
// check email address
// check age
if (isNaN(document.forms[0].elements[3].value))
{
alert ("Please enter a valid age!");
return false;
}
// check age range
if (parseInt(document.forms[0].elements[3].value) < 1 ||
parseInt(document.forms[0].elements[3].value) > 99)
{
alert ("Please enter a valid age!");
return false;
}
return true;
}
</script>
In the above code, if the name is checked once and then the password, the moment i leave the password field empty the error message is being displayed asking me to enter a password but when the alert box is clicked ok, all the values that i have entered in the form earlier disappear. How can i prevent this? I don't want to flush out the values if correct and if already entered.

Any help would be highly appreciated.

Thanks


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

What is the code where your validateForm function is called?
thewebdrivers
Forum Commoner
Posts: 41
Joined: Fri Aug 17, 2007 3:32 pm
Location: india
Contact:

Post by thewebdrivers »

looks like your form is submitting and is not check return data.

Please double check that in your form tag you have

Code: Select all

onclick="return validateForm();"
Post Reply