...

View Full Version : very simple form validation quetsion



jacka1
12-13-2010, 06:01 PM
Hi
I am trying to valid a simple address, that works fine apart from the fact that it will not allow forward slash in it "/" such as: Unit 12/13 some where.

In brief I want to change /^[\w ]+$/; to include a forward character in it.

I am new to javascript.

many thx.

p.s. every time I google it they find me email address validation dehhhhh



I use the following code:



function validateVat_No(fld) {
var error = "";
var illegalChars = /^[\w ]+$/; // allow letters, numbers, and spaces

if (fld.value !== "") {

if (!illegalChars.test(fld.value)) {
fld.style.background = 'Yellow';
error = "The Vat No contains illegal characters.\n";
}
}

else {
fld.style.background = 'White';

}
return error;
}

Philip M
12-13-2010, 06:22 PM
var illegalChars = /^[\w\s\/]+$/; // allow letters, numbers, spaces and /

Your "illegalChars" are in fact the characters which are allowed. And if it is supposed to validate a mailing address how does a VAT number come into it?
Note that that regex will allow a valid entry of one or more spaces. You should strip all leading/trailing spaces with

fld.value = fld.value.replace(/^\s+|\s+$/g,"");
You should also reject entries of less than a certain length - say 6 if it is supposed to be an address.

if (fld.value.length < 6) {
error = "The address appears to be incomplete";
}
All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

jacka1
12-14-2010, 09:12 AM
Hi
That didn't work unfortunately.
All I really want is to assign the variable to accept letters number space and "/" the things that one uses to enter the address in a form.

The code I should have posted, is..


function validateAddress(fld) {
var error = "";
var illegalChars = /^[\w +, ]+$/; // allow letters, numbers, and spaces

if (fld.value == "") {
fld.style.background = 'Yellow';
error = "You didn't enter an Address.\n";
} else if ((fld.value.length < 1) || (fld.value.length > 80)) {
fld.style.background = 'Yellow';
error = "The Address is the wrong length.\n";
} else if (!illegalChars.test(fld.value)) {
fld.style.background = 'Yellow';
error = "The Address contains illegal characters.\n";
} else {
fld.style.background = 'White';
}
return error;
}



Please feel free to suggest other characters that one may need to include in an address. eg. of address unit 12/13 some where.

Thx for your help.
:D

Philip M
12-14-2010, 09:48 AM
Sorry, there was a typo :o (although it still worked for the example). But
var illegalChars = /^[\w +, ]+$/; // allow letters, numbers, and spaces
is not the code I gave you. If you want to allow a + sign it must be escaped thus \+
If you want to allow a comma it is best to escape that as well \,
It might be a good idea to allow a hyphen \- and a full stop \. and an apostrophe \' (25-34, St.John's Street).



var address = "Unit 12/13, 25-34 St.John's Street"
var illegalChars = /^[\w\s\/\-\.\,\']+$/; // allow letters, numbers, spaces, full stops, hyphens, commas and /
if (!illegalChars.test(address)) {
alert ("There are illegal characters in the address")
}
else {
alert ("Address is OK")
}

Alternatively you could of course simply strip out any illegal characters.

Don't forget to strip leading and trailing spaces (essential!) , and possibly reduce multiple spaces to one single space.

jacka1
12-14-2010, 10:56 AM
Hi
My apologies, that worked fine. Hadn't tested it properly.
However I have one small problem.
I also test the data using PHP, just in case user has javascript turned off. I know this is not PHP forum but most coders also know about php coding.
I wish to allow "/" and "-" in the php code but doesn't seem to work.

original code:




function Address($Address)
{
if (!preg_match("/^[A-Za-z0-9]([A-Za-z0-9-,\s]*[A-Za-z0-9\s])*$/", $Address))


return false;

else
return true;
}


altered code:


function Address($Address)
{
if (!preg_match("/^[A-Za-z0-9]([A-Za-z0-9-,\/\-\s]*[A-Za-z0-9\s])*$/", $Address))


return false;

else
return true;
}


please help. Thx

Philip M
12-14-2010, 11:10 AM
You should post questions about PHP in the PHP forum.

But within a character class ([A-Za-z0-9-, a hyphen must be escaped.

jacka1
12-14-2010, 11:45 AM
Hi
Many thanks for your help greatly appreciated.
:thumbsup:
:D
p.s. any special character in a Character Class, is no longer a special character

Philip M
12-14-2010, 12:13 PM
p.s. any special character in a Character Class, is no longer a special character

The only special characters or metacharacters inside a character class are the closing bracket (]), the backslash (\), the caret (^) and the hyphen (-). The usual metacharacters are normal characters inside a character class, and do not need to be escaped by a backslash. To search for a star or plus, use [+*]. Your regex will still work fine if you escape the regular metacharacters inside a character class, but some consider that doing so reduces readability.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum