|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Help needed please
Hi there. I am using the code below to learn about form validation. When I insert correct data the form sends the data, and when i enter wrong data it clears the fields. What i am ideally looking for is for the code to keep the data on screen so that I can change it if i make a mistake. Here is the code below.
Thanks in advance for your help <script language="JavaScript1.2"> function CheckName(HoldName) { NoNumThere=true; for(i=0; i<HoldName.length; i++) { for(j=0; j<10; j++) { if(HoldName.charAt(i)==j.toString()) { NoNumThere=false; break; } } if(NoNumThere==false) { break; } } return NoNumThere; } function CheckMail(HoldMail) { IsValid=true; if(HoldMail.indexOf("@")<=0) { IsValid=false; } return IsValid; } function checkfields() { var AllFilled=true; for(i=0; i<3; i++) { if(visitor.elements[i].value.length==0) { alert("The field " + visitor.elements[i].name + " can not be left blank."); AllFilled=false; visitor.elements[i].focus; } } if(AllFilled==true) { var NameValid=true; var EmailValid=true; NameValid=CheckName(visitor.fullname.value); EmailValid=CheckMail(visitor.email.value); if(NameValid==false) { alert("Sorry, your name can not contain numbers."); visitor.fullname.focus; } if(EmailValid==false) { alert("Sorry, this does not seem like a valid email address."); } } if(NameValid==true & EmailValid==true) { alert("Worked"); } } </script> <body> <form name="visitor"> Enter your name: <input name="fullname" type="Text"> <br> Enter your email: <input name="email" type="Text"> <br> <input type="submit" name="submit" value="Submit" onClick="checkfields();"> <input type="Reset" name="reset" value="Reset"> </form> </body> </html> Allan ![]() |
|
#2
|
|||
|
|||
|
Anyone???
|
|
#3
|
||||
|
||||
|
Hi Allan! Welcome to the forums.
I had a look at your code... it looks like you might have more loops than you really need. You also may want to consider Regular Expressions because they're powerful and fun =) However, I found a quick way to fix the code you've already written. what I did was add some return statements after your alert messages and changed the submit button slightly. Code:
if(NameValid==false)
{
alert("Sorry, your name can not contain numbers.");
visitor.fullname.focus;
return false;
}
if(EmailValid==false)
{
alert("Sorry, this does not seem like a valid email address.");
return false;
}
}
if(NameValid==true & EmailValid==true)
{
alert("Worked");
return true;
}
Code:
<input type="submit" name="submit" value="Submit" onclick="return checkfields();"> Also note, in the future wrap your code with [code][/code] tags when posting... it makes it easier to read ![]()
__________________
Daryl's Homepage | My Blogroll | My Profile | Firefox supporter! DevArticles Forum Moderator "The net is a waste of time, and that's exactly what's right about it." -- William Gibson |
|
#4
|
|||
|
|||
|
It would be better to use the form's onsubmit event than to use the submit button's onclick event.
Code:
<form name="visitor" onsubmit="return checkfields();"> Edit: I fixed it. |
|
#5
|
|||
|
|||
|
i use
<form name="visitor" onsubmit="return checkfields();"> |
|
#6
|
|||
|
|||
|
Thanks ravs.
I forgot to change the "onclick" to "onsubmit" when I copy-and-pasted the event handler. I corrected my previous reply. |
|
#7
|
|||
|
|||
|
my pleasure
we all do mistakes |
|
#8
|
|||
|
|||
|
Thanks for the welcome and the help, the program works fine now. Thank you.
Allan |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > Help needed please |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|