Forms - How to check for all required fields are filled out

JavaScript and client side scripting.

Moderator: General Moderators

WizyWyg
Forum Commoner
Posts: 92
Joined: Tue Aug 06, 2002 7:20 pm

Post by WizyWyg »

so this should worK

Code: Select all

var reqfields = Array("Email::Email", "Firstname::First Name","Lastname::Last Name","month::Birthdate Month","day::Birthdate Day","year::Birthdate Year","Address::Adress","City::City","State::State","Zip::Zip");
function checkIt()
{
   var msg = "The Following Information Needs Attention, Please Fix And Resubmit \n\n";
   var mssg = "Only letters and numbers are allowed for these fields \n\n";
   var str = /^[\w+\d+]/;
   for (var i=0; i<reqfields.length; i++)
   {
      thStuff = reqfields[i].split("::");
      thText = thStuff[1];
      thName = thStuff[0];
      theField = document.g_form[thName];
      if (theField.value == "")
      
      msg += thText+" Is REQUIRED \n";
      else if(g_form[thName])
      {
      if(!str.test(document.g_form[thName].value))
         msg += thText+" Can Only Have Letters Or Numbers\n";
      }
   }
   if (msg.length>90)
   {
      alert(msg);
      return false;
   }
   return true;
}
WizyWyg
Forum Commoner
Posts: 92
Joined: Tue Aug 06, 2002 7:20 pm

Post by WizyWyg »

yup it did
thanks alot burrito.
Im very unfamiliar with what was in that code.

what if I want it accept periods, commas and pound signs for the fields (since addresses can contain these symbols)
Last edited by WizyWyg on Fri Feb 03, 2006 10:03 pm, edited 1 time in total.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

ok, tested.

I had to refine my pattern a bit so now str should look like this:

Code: Select all

var str = /[^a-zA-Z0-9\s]/;
with that change it works perfectly.

http://www.burritostand.com/test.htm
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

guess we posted at the same time....

make sure you change the str variable to what I just posted above to really make it work ;)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I just read your code:

you need to identify which fields can only have numbers or letters and put them in your else if block...otherwise it will check all of them and that obviously won't work for email addresses etc...
WizyWyg
Forum Commoner
Posts: 92
Joined: Tue Aug 06, 2002 7:20 pm

Post by WizyWyg »

Code: Select all

var str = /^[\w+\d+]/;
this actually worked (allows #@., ) and such

but now when i do complete the entire form, it doesn't submit. Still pops up wit the "you have blahblah" but it lists nothing
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I wouldn't use that pattern...use the second one I posted.

you might need to adjust the msg.length check as I'm sure it changed with all of the stuff that we've done.
WizyWyg
Forum Commoner
Posts: 92
Joined: Tue Aug 06, 2002 7:20 pm

Post by WizyWyg »

Burrito wrote:I wouldn't use that pattern...use the second one I posted.
Then it makes every field wrong / error message out even if i take out the ! from the !str in the if statement, since I need all the fields to accept at least @#,.
WizyWyg
Forum Commoner
Posts: 92
Joined: Tue Aug 06, 2002 7:20 pm

Post by WizyWyg »

sorry, now its allowing all characters ( ie ()*&^%$!~ )
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Oh yeah, I also took out the 'not'

here is the code I had that worked perfectly

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
	<title>Untitled</title>
<script>
var reqfields = Array("Email::Email", "Firstname::First Name"); 
function checkIt() 
{ 
	var msg = "The Following Information Needs Attention, Please Fix And Resubmit \n\n"; 
	var mssg = "Only letters and numbers are allowed for these fields \n\n"; 
	var str = /[^a-zA-Z0-9\s]/; 
	for (var i=0; i<reqfields.length; i++) 
	{ 
		thStuff = reqfields[i].split("::"); 
		thText = thStuff[1]; 
		thName = thStuff[0]; 
		theField = document.g_form[thName]; 
		if (theField.value == "") 
	   
		msg += thText+" Is REQUIRED \n"; 
		else if(thName == "Firstname")
		{
		if(str.test(document.g_form[thName].value))
			msg += thText+" Can Only Have Letters Or Numbers";
		}
	} 
	if (msg.length>90) 
	{ 
		alert(msg); 
		return false; 
	} 
	return true; 
}
</script>
</head>

<body>
<form name="g_form">
<input type="text" name="Email"><br>
<input type="text" name="Firstname"><br>
<input type="button" onClick="checkIt()" value="go">

</form>


</body>
</html>
WizyWyg
Forum Commoner
Posts: 92
Joined: Tue Aug 06, 2002 7:20 pm

Post by WizyWyg »

Burrito wrote:Oh yeah, I also took out the 'not'

here is the code I had that worked perfectly

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
	<title>Untitled</title>
<script>
var reqfields = Array("Email::Email", "Firstname::First Name"); 
function checkIt() 
{ 
	var msg = "The Following Information Needs Attention, Please Fix And Resubmit \n\n"; 
	var mssg = "Only letters and numbers are allowed for these fields \n\n"; 
	var str = /[^a-zA-Z0-9\s]/; 
	for (var i=0; i<reqfields.length; i++) 
	{ 
		thStuff = reqfields[i].split("::"); 
		thText = thStuff[1]; 
		thName = thStuff[0]; 
		theField = document.g_form[thName]; 
		if (theField.value == "") 
	   
		msg += thText+" Is REQUIRED \n"; 
		else if(thName == "Firstname")
		{
		if(str.test(document.g_form[thName].value))
			msg += thText+" Can Only Have Letters Or Numbers";
		}
	} 
	if (msg.length>90) 
	{ 
		alert(msg); 
		return false; 
	} 
	return true; 
}
</script>
</head>

<body>
<form name="g_form">
<input type="text" name="Email"><br>
<input type="text" name="Firstname"><br>
<input type="button" onClick="checkIt()" value="go">

</form>


</body>
</html>
its now accepting all characters if I use this.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

the email box (top box) should.

the name box (second box) should not.... 8O
WizyWyg
Forum Commoner
Posts: 92
Joined: Tue Aug 06, 2002 7:20 pm

Post by WizyWyg »

the only characters i need it to accept for all fields are
a-z
A-Z
0-9
@#-.,

right now its either not allowing the last set,
or letting all characters through.

my current code

Code: Select all

var reqfields = Array("Email::Email", "Firstname::First Name", "Lastname::Last Name", "month::Birthdate Month", "day::Birthdate Day", "year::Birthdate Year", "Address::Address", "City::City", "State::State", "Zip::Zip");
function checkIt() {
	var msg = "The following fields were either left blank or contained special characters, please fix and resubmit. \n\n";
//	var str = /^[\w+\d+]/;
	var str = /[^a-zA-Z0-9\s]/;
	for (var i = 0; i<reqfields.length; i++) {
		thStuff = reqfields[i].split("::");
		thText = thStuff[1];
		thName = thStuff[0];
		theField = document.g_form[thName];
		if (theField.value == "") {
			msg += thText+" Is REQUIRED \n";
		} else if (g_form[thName]) {
			if (str.test(document.g_form[thName].value)) {
				msg += thText+" Can Only Have Letters Or Numbers\n";
			}
		}
	}
	if (msg.length>255) {
		alert(msg);
		return false;
	}
	return true;
}
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

ok then add those last few to your pattern

you'll need to escape your '.' with a backslash.
WizyWyg
Forum Commoner
Posts: 92
Joined: Tue Aug 06, 2002 7:20 pm

Post by WizyWyg »

Code: Select all

var reqfields = Array("Email::Email", "Firstname::First Name", "Lastname::Last Name", "month::Birthdate Month", "day::Birthdate Day", "year::Birthdate Year", "Address::Address", "City::City", "State::State", "Zip::Zip");
function checkIt() {
	var msg = "The following fields were either left blank or contained special characters, please fix and resubmit. \n\n";
//	var str = /^[\w+\d+]/;
	var str = /[^a-zA-Z0-9\s@#-\.,]/;
	for (var i = 0; i<reqfields.length; i++) {
		thStuff = reqfields[i].split("::");
		thText = thStuff[1];
		thName = thStuff[0];
		theField = document.g_form[thName];
		if (theField.value == "") {
			msg += thText+" Is REQUIRED \n";
		} else if (g_form[thName]) {
			if (str.test(document.g_form[thName].value)) {
				msg += thText+" Can Only Have Letters Or Numbers\n";
			}
		}
	}
	if (msg.length>255) {
		alert(msg);
		return false;
	}
	return true;
}
is still allowing other characters through ( ie ;()*&^%$!~ )
Post Reply