Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form needs fields completed & check unwanted characters

Status
Not open for further replies.

WizyWyg

Technical User
Jan 31, 2001
854
0
0
JP
I have a form that needs to have its fields completed, and check for unwanted characters:

Code:
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@#-\.,]/;
//	var str = /[^~!$%^&*()_+|`{}[]:;<>?]/;
	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;
}

However, with this its allowing all characters through.
I only need these characters to be allowed:

a-z
A-Z
0-9
@#-.,

Anything else should pop up the warning message.
 

[tt]
theField = document.g_form[red].elements[/red][thName];
if (theField.value == "") {
msg += thText+" Is REQUIRED \n";
} else[highlight] [/highlight]{
if (str.test(document.g_form[red].elements[/red][thName].value)) {
msg += thText+" Can Only Have Letters Or Numbers\n";
}
}
[/tt]
 
>However, with this its allowing all characters through.
If you mean by that it is submitted in a onsubmit handler, then it is the return value.
[tt]
onsubmit="return checkIt()"[/tt]

and then you have to return false to where only only msg is scripted.
[tt]
theField = document.g_form.elements[thName];
if (theField.value == "") {
msg += thText+" Is REQUIRED \n";
[red]alert(msg);
return false;[/red]
} else {
if (str.test(document.g_form.elements[thName].value)) {
msg += thText+" Can Only Have Letters Or Numbers\n";
[red]alert(msg);
return false;[/red]
}
}
[/tt]
 
Thanks but both CODES are still allowing unwanted characters through

only @#-,. a-z A-Z 0-9 are supposed to be allowed
 
Look again on you onsubmit line. What do you script there? The problem is somewhere else.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top