Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-24-2009, 10:19 AM   PM User | #1
richyrich38
New Coder

 
Join Date: Jul 2009
Posts: 10
Thanks: 2
Thanked 0 Times in 0 Posts
richyrich38 is an unknown quantity at this point
form validation check if email or phone number entered??

HI Guys, not the greatest with javascript

I have this form validation code

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?
richyrich38 is offline   Reply With Quote
Old 07-24-2009, 10:46 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
This ought to move you forward:-

Code:
<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)

Last edited by Philip M; 07-24-2009 at 10:59 AM.. Reason: Typo
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
richyrich38 (07-24-2009)
Old 07-24-2009, 10:51 AM   PM User | #3
richyrich38
New Coder

 
Join Date: Jul 2009
Posts: 10
Thanks: 2
Thanked 0 Times in 0 Posts
richyrich38 is an unknown quantity at this point
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,
richyrich38 is offline   Reply With Quote
Old 07-24-2009, 12:23 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Form validation has been covered a zillion times in this forum.
Philip M is offline   Reply With Quote
Old 07-24-2009, 02:36 PM   PM User | #5
richyrich38
New Coder

 
Join Date: Jul 2009
Posts: 10
Thanks: 2
Thanked 0 Times in 0 Posts
richyrich38 is an unknown quantity at this point
Fixed now

Last edited by richyrich38; 07-24-2009 at 03:12 PM.. Reason: Fixed problem
richyrich38 is offline   Reply With Quote
Reply

Bookmarks

Tags
form validation, javascript.

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:46 AM.


Advertisement
Log in to turn off these ads.