Form validation

This is a discussion on "Form validation" within the JavaScript Forum section. This forum, and the thread "Form validation are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack (1) Thread Tools
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old Jul 8th, 2005, 17:33
New Member
Join Date: Jul 2005
Location: Thousand Oaks, CA
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Form validation

I'm sure this is a total newbie question but I'm going to ask anyways because I haven't been able to find the answer anywhere else.

How can I check input in a form to see if it complies with a specific format? For example, if I want to make sure that they have input a valid email address, how do I check that it has an "@" and a ".", or for a phone number how can I check that they have input a number that is 10 digits long and no longer? and that it's actually numbers and not a sentence?

Also, how can I set the size of the input boxes? I tried setting maxlength and different settings but nothing seemed to set it right.

I feel like I should send the code, so I'm going to post the form... even though I'm pretty sure my question is pretty self explanatory without the code:

Code: Select all
<html>
    <head>
		<script language="javascript">
			function validate(form)
			{
				if (form.name.value.length == 0)
					{
					alert("Please enter your name.");
					form.name.focus();
					return false;
					}
				if (form.hospital.value.length == 0)
					{
					alert("Please enter the name of the hospital you represent.");
					form.hospital.focus();
					return false;
					}
				if (form.email.value.length == 0)
					{
					alert("Please enter your email address.");
					form.email.focus();
					return false;
					}
				if (form.location.value.length == 0)
					{
					alert("Please enter your location.");
					form.location.focus();
					return false;
					}
				if (form.phone.value.length == 0)
					{
					alert("Please enter your phone number.");
					form.phone.focus();
					return false;
					}
				if (form.calltime.value.length == 0)
					{
					alert("Please enter a convenient time for us to call you.");
					form.calltime.focus();
					return false;
					}
				return true;
			}
		</script>
	</head>
    <body>	
       	

Please fill out the form below so we can contact you to set up an internet demo.</p>

        <form name="form" action="mailform.php" method="POST">

            

(Fields marked with a * are required)</p>
            

*Name: 
              <input type="text" id="name" name="name"/>
            </p>
            

*Email: 
              <input type="text" da id="email" name="email"/></p>
			

*Hospital: 
			  <input type="text" id="hospital" name="hospital"/></p>
			

*Location: 
			  <input type="text" id="location" name="location"/></p>
			

*Phone number: 
			  <input id="phone" type="text" name="phone"/></p>
            

*Best time to Call: 
              <input type="text" id="calltime" name="calltime"/></p>

            

Comments:</p>
            

<textarea id="comments" name="comments"></textarea></p>

            

<input type="submit" value="Send!" id="submit" name="submit" onclick="return validate(form)"/></p>

        </form>
          </body>
</html>
Thanks!
-Brittny
Webmaster Theronyx.com[/code]
Reply With Quote

  #2 (permalink)  
Old Jul 8th, 2005, 18:37
New Member
Join Date: Jul 2005
Location: Thousand Oaks, CA
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
semi-solution

I've found part of my solution and thought i would post it here in case anyone else finds this sometime and they are looking for the answer too.

I found an advanced email check at http://www.javascriptkit.com/script/...2/acheck.shtml.

I modified it to fit in with my solution. Here's my updated code (just the javascript since that's the only thing that's changed):
Code: Select all
		<script language="javascript">
			function validate(form)
			{
				if (form.name.value.length == 0)
					{
					alert("Please enter your name.");
					form.name.focus();
					return false;
					}
				if (form.hospital.value.length == 0)
					{
					alert("Please enter the name of the hospital you represent.");
					form.hospital.focus();
					return false;
					}
				if (form.email.value.length == 0)
					{
					alert("Please enter your email address.");
					form.email.focus();
					return false;
					}
				return checkemail();
				if (checkemail ==false)
					{
						form.email.focus();
						return false;
					}
				if (form.location.value.length == 0)
					{
					alert("Please enter your location.");
					form.location.focus();
					return false;
					}
				if (form.phone.value.length == 0)
					{
					alert("Please enter your phone number.");
					form.phone.focus();
					return false;
					}
				if (form.calltime.value.length == 0)
					{
					alert("Please enter a convenient time for us to call you.");
					form.calltime.focus();
					return false;
					}
				return true;
			}
			function checkemail()
			{
			var str=document.form.email.value;
			var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
				if (filter.test(str))
					{
					return true;
					}
				else
					{
					alert("Please input a valid email address!");
					return false;
					}
			}
I still don't really understand how this code works so if someone could explain what the filter.test statement does I'd appreciate it. I'm also still working on getting validation for phone numbers, but i think if someone can help me understand the previous script I think I'll be able to do the phone number.

Thanks!!
-Brittny
Web developer at www.theronyx.com
Reply With Quote
  #3 (permalink)  
Old Jul 8th, 2005, 19:46
New Member
Join Date: Jul 2005
Location: Thousand Oaks, CA
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
solved!

Well, I feel really dumb that after posting my question I solved it myself...

But I figure I should at least post my solution so that anyone else with a similar question can find it...

So what I ended up doing was breaking the phone number section down into three parts and then did a number check (another script that i found at www.javascriptkit.com) on each set of numbers. That way I avoided checking for characters to separate the digits and I could easily check for a phone number.

Code: Select all
		<script language="javascript">
			function validate(form)
			{
				if (form.name.value.length == 0)
					{
					alert("Please enter your name.");
					form.name.focus();
					return false;
					}
				if (form.hospital.value.length == 0)
					{
					alert("Please enter the name of the hospital you represent.");
					form.hospital.focus();
					return false;
					}
				if (form.email.value.length == 0)
					{
					alert("Please enter your email address.");
					form.email.focus();
					return false;
					}
				return checkemail();
				if (checkemail ==false)
					{
						form.email.focus();
						return false;
					}
				if (form.location.value.length == 0)
					{
					alert("Please enter your location.");
					form.location.focus();
					return false;
					}
				if (form.phone1.value.length == 0)
					{
					alert("Please enter your phone number.");
					form.phone1.focus();
					return false;
					}
				var x= form.phone1.value;
				return checknum();
				if (checknum == false)
				{
					form.phone.focus();
					return false;
				}
				if (form.phone2.value.length == 0)
					{
					alert("Please enter your phone number.");
					form.phone2.focus();
					return false;
					}
				var x= form.phone2.value;
				return checknum();
				if (checknum == false)
				{
					form.phone.focus();
					return false;
				}
				if (form.phone3.value.length == 0)
					{
					alert("Please enter your phone number.");
					form.phone3.focus();
					return false;
					}
				var x= form.phone3.value;
				return checknum();
				if (checknum == false)
				{
					form.phone.focus();
					return false;
				}
				return checknum();
				if (checknum == false)
				{
					form.phone.focus();
					return false;
				}
				if (form.calltime.value.length == 0)
					{
					alert("Please enter a convenient time for us to call you.");
					form.calltime.focus();
					return false;
					}
				return true;
			}
			function checkemail()
			{
			var str=document.form.email.value;
			var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
				if (filter.test(str))
					{
					return true;
					}
				else
					{
					alert("Please input a valid email address!");
					return false;
					}
			}
			function checknum()
			{
				var anum=/(^\d+$)|(^\d+\.\d+$)/;
				if (anum.test(x))
				{
					testresult=true
				else
				{
					alert("Please input a valid phone number!");
				testresult=false
				}

				}
		</script>
I'd still really like to understand what exactly is happening with the .test so if someone could explain it that would be great.

Thanks!
-Brittny
Webmaster at www.theronyx.com
Reply With Quote
Reply

Tags
form, validation

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.webforumz.com/javascript-forum/2606-form-validation.htm
Posted By For Type Date
flash form validation This thread Refback Sep 11th, 2006 07:15

Similar Threads
Thread Thread Starter Forum Replies Last Post
Form validation help! psycho wolvesbane JavaScript Forum 16 Feb 12th, 2008 16:40
Form Validation cjrollo Flash & Multimedia Forum 0 Feb 22nd, 2007 17:33
Form Validation feebee JavaScript Forum 1 Aug 3rd, 2006 16:12
PHP Form Validation. kaz PHP Forum 2 Jul 22nd, 2006 20:47
PHP Form Validation ??? j4mes_bond25 PHP Forum 2 May 31st, 2006 23:08


All times are GMT. The time now is 19:18.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43