Page 3 of 4

Posted: Fri Feb 03, 2006 9:49 pm
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;
}

Posted: Fri Feb 03, 2006 9:57 pm
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)

Posted: Fri Feb 03, 2006 9:59 pm
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

Posted: Fri Feb 03, 2006 10:01 pm
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 ;)

Posted: Fri Feb 03, 2006 10:04 pm
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...

Posted: Fri Feb 03, 2006 10:09 pm
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

Posted: Fri Feb 03, 2006 10:22 pm
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.

Posted: Fri Feb 03, 2006 10:25 pm
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 @#,.

Posted: Fri Feb 03, 2006 10:30 pm
by WizyWyg
sorry, now its allowing all characters ( ie ()*&^%$!~ )

Posted: Fri Feb 03, 2006 10:31 pm
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>

Posted: Fri Feb 03, 2006 10:34 pm
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.

Posted: Fri Feb 03, 2006 10:36 pm
by Burrito
the email box (top box) should.

the name box (second box) should not.... 8O

Posted: Fri Feb 03, 2006 10:36 pm
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;
}

Posted: Fri Feb 03, 2006 10:40 pm
by Burrito
ok then add those last few to your pattern

you'll need to escape your '.' with a backslash.

Posted: Fri Feb 03, 2006 10:44 pm
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 ;()*&^%$!~ )