...

View Full Version : form validation check if email or phone number entered??



richyrich38
07-24-2009, 11:19 AM
HI Guys, not the greatest with javascript

I have this form validation code


function check_email(mailstring)
{
valid = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
for(i=0; i < mailstring.length ;i++)
{
if (valid.indexOf(mailstring.charAt(i))<0)
{
return (false);
}
}
if (document.images)
{
one = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
if (!mailstring.match(one) && mailstring.match(two))
{
return (-1);
}
}
}

function validate()
{

if (document.mailpage.contact_name.value=="")
{
alert("Name not entered")
return false
}
if (document.mailpage.contact_address.value=="")
{
alert("Address not entered")
return false
}
if (document.mailpage.contact_otherinfo.value=="")
{
alert("Operational Details / Other Information was not entered")
return false
}

if (!check_email(document.mailpage.contact_email.value))
{
alert("Invalid Email address")
return false
}
if (document.mailpage.contact_phone.value=="")
{
alert("Phone not entered")
return false
}



}

at the moment its checks that someting has been enetered in all these fields,

what would be the best way to check that the email or phone nummber had been entered?? will not send if nothing enetered, but will send if one or both have been enetered?

Philip M
07-24-2009, 11:46 AM
This ought to move you forward:-


<script type = "text/javascript">

function validate() {

if (document.mailpage.contact_name.value=="") {
alert("Name not entered");
return false
}
if (document.mailpage.contact_address.value=="") {
alert("Address not entered");
return false
}
if (document.mailpage.contact_otherinfo.value=="") {
alert("Operational Details / Other Information was not entered");
return false
}

var f = 0;
var em = document.mailpage.contact_email.value;

if (em != "") { // if em contains a value
if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@(([\w\-]?)+\.)+([a-z]{2,4})$/i.test(em))) {
alert("Invalid Email address - please re-enter");
document.mailpage.contact_email.value = "";
document.mailpage.contact_email.focus();
return false;
}
else {f++} // an email address has been entered
}

var ph = document.mailpage.contact_phone.value;
ph = ph.replace(/[^0-9\-/,""); //strip non-numeric characters except hyphen
if (ph != "") { // if ph contains a value after stripping
if (ph.length < 10) {
alert ("Phone number not valid - please re-enter");
document.mailpage.contact_phone.value = "";
document.mailpage.contact_phone.value.focus();
return false
}
else {f++} // a phone number has been entered
}

if (f==0) {
alert ("You must enter either your email address or your phone number, or both");
return false;
}
else {return true}
}

</script>


Note that validation of the type if document.mailpage.contact_name.value=="") { is barely worthy of the name, and is virtually worthless, as even a single space or a ? will return false (i.e. pass the validation). You should
a) strip leading and trailing spaces
b) remove inappropriate characters (a name can only be a-z hyphen apostrophe space)
c) check for a minimum length (say at least 3 alpha characters)




"In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)

richyrich38
07-24-2009, 11:51 AM
Thank you for the help and advice.

Im very new to this and always looking at was to improve my coding.

sorry to be a pain, do you know any good links for me read on Good form validation,

Philip M
07-24-2009, 01:23 PM
Form validation has been covered a zillion times in this forum.

richyrich38
07-24-2009, 03:36 PM
Fixed now



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum