Combine Required Fields & Validating Correct Email

liunx

Guest
Hi,<br />
<br />
Im kinda in a hurry to get this over with so i dont have much time or energy to "play" around with it anymore..so...<br />
<br />
Here's the thing I have two scripts one validates that all required fields - name, email, tel.nos..etc. are accomplished before it gets Submitted (via poprompt)...the other one, makes sures that the email address is correctly typed in..as in with the @ sign and domain are in order...<br />
<br />
I've combined the two into one script (all fields are required), please see attached..but I can only make one work...how can i properly combine the two? Right now with the attached text script..it validates ONLY that Required fields are complete...<br />
I almost got it to work validating required fields and that a valid email is entered...but then...it only validated until the email add. w/o validating the rest of the fields :(<br />
<br />
The "perfect" sequence would be...first check that the Name field is filled up, and then check that a valid email address has been put in...(coz i dont want to have them put in a bogus email from the start..even if they've completed the other fields b4 submission...although, i could probably live with having them to go back and check the email field again..)<!--content-->in your command button put onclick"errorcheck()"<br />
<br />
your java script for required feilds and email validator would be<br />
<br />
<script language="JavaScript"><br />
<!-- Hide<br />
//error check function<br />
function errorcheck()<br />
{<br />
var requiredfieldserrormessage = " ";<br />
var emailentered = " ";<br />
var nameentered = " ";<br />
//feild error checking<br />
nameentered = document.forms['yourformname']<br />
.elements['name'].value<br />
emailentered = document.forms['yourformname']<br />
.elements['email'].value<br />
if ((!emailentered) || (!nameentered)<br />
{<br />
if (!nameentered)<br />
{<br />
requiredfieldserrormessage = "- Please enter your name\r"<br />
}<br />
if (!emailentered)<br />
{<br />
requiredfieldserrormessage =<br />
requiredfieldserrormessage + "- Please enter your email\r"<br />
}<br />
}<br />
//email validator<br />
if emailentered)<br />
{<br />
emailentered = document.forms['formname']<br />
.elements['emai'].value;<br />
emailvalue = new string (emailentered);<br />
emailhasat = email.value.indexof("@");<br />
emailhasperiod = email.value.indexof(".");<br />
email ((emailhasat == -1) || (emailhasperiod == -1))<br />
{<br />
emailerrormessage ="-Please retype your email\r";<br />
}<br />
}<br />
//error messages<br />
if ((requiredfieldserrormessage) || (emailerrormessage) <br />
{<br />
alertmessage="Oops! There is a little trouble with the information you provided\r";<br />
//build the message<br />
alertmessage= alertmessage + requiredfeildserrormessage;<br />
alertmessage= alertmessage + emailerrormessage;<br />
alert (alertmessage);<br />
}<br />
else<br />
{<br />
document.forms['yourformname'].submit();<br />
}<br />
}<br />
//end hiding --><br />
</script><br />
well there it is. Its a beautiful thing isnt it? That should work, be sure to put your form name in there where I have it listed 'formname' otherwise this thing is worthless, also modify your object names.<!--content-->Gee, thanks! But this is a totally new script right?..im sure it works...but i kinda prefer to have my origial attached script looked into and fixed...<br />
<br />
I'd much prefer the one i posted coz it validates all the fields (not only the name..) and at the same time after validating that all fields are filled up...it ensures that the correct email format is followed..<br />
<br />
Besides, it'll teach me...or at least give me an idea on how to combine two quite similar but diff. scripts...<!--content-->This script is a combo of two alredy. Its a script that checks for extrainious data and the validator. You can just add the other things you want validated by copying and pasting and changing two or three names.<!--content-->Hey thanks...i didnt actually use your suggested script..but I found a combined script w/c was a bit closer to the original..txt scriptfile attached if you're interested...<br />
<br />
However, there is a bug in it..see if you can find out what it is..it's in the Name field :)<br />
<br />
Ok, i'll just tell you: if you put in an entire name with two or more spaces in between...ex. Richard E. Small it returns an invalid username prompt...<br />
<br />
Can you fix that?...please don't give me another totally different script...<br />
<br />
Cheers!<br />
<br />
PS: Also, i think there's a better way to present the validation for the Telephone field...<!--content-->Ok its definatly you string that is doing this.<br />
<head><br />
<br />
<title>Regular Expressions for Form Field Validation</title><br />
<br />
<script language="JavaScript"><!--<br />
function isEmail(string) {<br />
<br />
if (!string) return false;<br />
var iChars = "*|,\":<>[]{}`\';()&$#%";<br />
<br />
for (var i = 0; i < string.length; i++) {<br />
if (iChars.indexOf(string.charAt(i)) != -1)<br />
return false;<br />
}<br />
return true;<br />
} <br />
function isProper(string) {<br />
<br />
if (!string) return false;<br />
var iChars = "*|,\":<>[]{}`\';()@&$#%";<br />
<br />
for (var i = 0; i < string.length; i++) {<br />
if (iChars.indexOf(string.charAt(i)) != -1)<br />
return false;<br />
}<br />
return true;<br />
} <br />
<br />
function isNumber(string) {<br />
<br />
if (!string) return false;<br />
var iChars = "*|,\":<>[]{}`\';@&$#%abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";<br />
<br />
for (var i = 0; i < string.length; i++) {<br />
if (iChars.indexOf(string.charAt(i)) != -1)<br />
return false;<br />
}<br />
return true;<br />
} <br />
<br />
function isReady(form) {<br />
if (isEmail(form.requiredemail.value) == false) {<br />
alert("Please enter a valid email address.");<br />
form.requiredemail.focus();<br />
return false;<br />
}<br />
if (isProper(form.requiredname.value) == false) {<br />
alert("Please enter a valid username.");<br />
form.requiredname.focus();<br />
return false;<br />
}<br />
if (isNumber(form.requiredtelephone.value) == false) {<br />
alert("Please enter a valid telephone no.");<br />
form.requiredtelephone.focus();<br />
return false;<br />
}<br />
return true;<br />
}<br />
//--></script><br />
<br />
<script language="JavaScript1.2"><br />
function isEmail(string) {<br />
if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)<br />
return true;<br />
else<br />
return false;<br />
}<br />
<br />
function isProper(string) {<br />
if (string.search(/^\w+( \w+)?$/) != -1)<br />
return true;<br />
else<br />
return false;<br />
}<br />
//--></script><br />
</head><br />
<br />
<body><br />
<body onLoad="this.document.form_name.requiredemail.focus();"><br />
<form name="form_name" onSubmit="return isReady(this)"><br />
<table cellpadding=0 cellspacing=5 border=0><tr><br />
<td align="left">Your Name:</td><td align="left"><input type="text" name="requiredname"></td><br />
</tr><tr><br />
<td align="left">Your Email Address:</td><td align="left"><input type="text" name="requiredemail" style="BACKGROUND-COLOR: rgb(255,255,0); COLOR: rgb(0,0,255);font-family: Verdana;"></td><br />
</tr><tr><br />
<td align="left">Your Telephone Number:</td><td align="left"><input type="text" name="requiredtelephone"></td><br />
</tr><tr><br />
<td align="left">Postal Address (optional):</td><td align="left"><textarea name="postaladdress" rows="10" cols="50" style="BACKGROUND-COLOR: rgb(255,200,16); COLOR: rgb(0,0,0)"></textarea></td><br />
</tr><tr><br />
<td><input type="submit" value="Submit"></td><br />
</tr></table><br />
</form><br />
<br />
</body><br />
</html><br />
<br />
I have not found the error yet but Ill hunt for it more later. But what I whould do is make anothe validation feild and add a text box for the last name. Thats just how I have always done it. Last first and middle enitial all separate.<!--content-->
 
Back
Top