...

View Full Version : Optional Email address validation



holty
03-10-2004, 09:59 AM
Hi

I can validate an email address but can't make it optional if the field isn't filled in - has anyone any ideas on what to add to my code?



<html>
<head>
<script Language="JavaScript">

function isEmailAddr(email)
{
var result = false;
var theStr = new String(email);
var index = theStr.indexOf("@");
if (index > 0)
{
var pindex = theStr.indexOf(".",index);
if ((pindex > index+1) && (theStr.length > pindex+1))
result = true;
}
return result;
}

function validRequired(formField,fieldLabel)
{
var result = true;

if (formField.value == "")
{
alert('Please enter a value for the "' + fieldLabel +'" field.');
formField.focus();
result = false;
}

return result;
}

function validEmail(formField,fieldLabel,required)
{
var result = true;

if (required && !validRequired(formField,fieldLabel))
result = false;

if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
{
alert("Please enter a complete email address in the form: yourname@yourdomain.com");
formField.focus();
result = false;
}

return result;

}

function validateForm(update)
{


if (!validEmail(update.email,"Email Address", false))
return false;

return true;

}

</script>
</head>
<body>
<form name="update" action="test.php" method="post" onsubmit="return validateForm(this)">
<table>
<tr>
<td width="40%"><b>Email address:</b></td>
<td width="60%"><input type="text" name="email" maxlength="100" size="20"></td>
</tr>
<tr>
<td width="100%" colspan="2"><center><input type="submit" name="Update" value="Update"></center></td>
</tr>
</table>
</form>
</body>
</html>


Thanks

Willy Duitt
03-10-2004, 03:11 PM
This script I have handy may give you some ideas.


<script type="text/javascript">
<!--//
var agree = "";
function validateForm(form){
for(var i=0; i<form.elements.length; i++){
if(form.elements[i].value=="" && form.elements[i].name!="email"){
var msg = 'Please enter a value in '+form.elements[i].name.toUpperCase();
alert(msg);
form.elements[i].focus();
return false;
}
}

if(agree==""){
var msg = 'Would you like to enter your email address?';
agree = confirm(msg);
if(agree){
form.email.focus();
return false;
}
else{
form.email.value="";
return true;
}
}

var goodEmail = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/.test(form.email.value);
if(agree && form.email.value=="" || !goodEmail){
var msg = 'Are you sure you wish to enter your email address?';
msg+= '\nIf so, please enter a valid email address.';
agree = confirm(msg);
if(agree){
form.email.value = '';
form.email.focus();
return false;
}
else{
form.email.value="";
return true;
}
}
return true;
}
//-->
</script>
</head>

<body>
<form method="post" action="mailto:" onsubmit="return validateForm(this)">
NAME: <input type="text" name="name"><br>
EMAIL: <input type="text" name="email"><br>
ADDRESS: <input type="text" name="address"><br>
<input type="submit" value="Send">
<input type="reset" value="Reset">
</form>


.....Willy

holty
03-11-2004, 08:31 AM
Cheers willy

do u know how to do if so that if they don't fill in an email address it passes through and if they do fill it in it will validate?

as you can see i have the code to validate but it seems to validate even if no email address is inserted!

Thanks

Willy Duitt
03-11-2004, 05:56 PM
That is what the script I posted does. If you do not want to confirm if the user wants to fill in the email addrress. Remove the if(agree) conditional statement and any reference to it.

Also the below does much more validating in one line than the 20 or so lines you are using to validate @ and/or length is more than three. I would recommend that you use this regular expression instead of the routine you are using.
/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/.test(form.email.value);

FWIW: If you would like your script to validate an empty text box. Remove this conditional statement and any reference to it.
if (formField.value == "")


.....Willy



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum