...

View Full Version : Could I have some regexp help? :)



APD1993
03-19-2012, 09:15 PM
I was wondering if anyone could help me with regexp:

1)I would like it so that both a first and last name can be entered and that both ' and - can be included
2)I would like it so that addresses have to start with a number and then each word afterwards has to start with a capital letter and can include full stops, ' and -

Any help is appreciated! :)

Old Pedant
03-19-2012, 10:21 PM
var nameRE = /^[A-Z]([a=z]|[\'\-][A-Z])*[a-z](\s[A-Z]([a=z]|[\'\-][A-Z])?$/;

You didn't ask for it, but...

That insists that the first and last name start with an upper case letter and otherwise consist of only lower case letters, EXCEPT that if ' or - is included the letter after the ' or - must also be upper case.

So:
John O'Brien works
Mallory Spence-Waite works
JD Powers does NOT work
Bob McFarlane does NOT work (oops! do we need to handle this, too?)
April White-O does NOT work (can't end with upper case letter)

If you don't care about being so fancy with upper/lower case:


var nameRE = /^[a-z][a=z\'\-]*[a-z](\s[a-z][a=z\'\-]*[a-z])?$/i; // i means ignore case


************


var addrRE = /^\d+(\s[A-Z][a-z\'\-\.]*[a-z\.])+$/

Again, I modified your requirements slightly. That allows ' or - or . anywhere in a "word" but only allows the period at the end of the word.

123 Fourth Ave. is okay
123 W. Whistle-stop is okay
777 Jone's Rd. is okay
Jackson And Pine is NOT okay (I think it should be)
Box 666 is NOT okay (I think it should be)
1 Saints' Street is NOT okay

You will have to decide if you want the rules to be that restrictive, in actuality.

Old Pedant
03-19-2012, 10:24 PM
By the way, my own address would fail your test.

7713 SE 199th Ave.

Here, avenues are always PREFIXED with the N,E,S,W,NE,SE,SW,NW directional indicator.

Streets have the directional indicator on the end:

441 20th St. NE

But in any case, streets and avenues are overwhelmingly numbered, not named, which kills your system completely. And the directional indicator is always capitalized, even when it is two letters.

APD1993
03-19-2012, 10:28 PM
By the way, my own address would fail your test.

7713 SE 199th Ave.

Here, avenues are always PREFIXED with the N,E,S,W,NE,SE,SW,NW directional indicator.

Streets have the directional indicator on the end:

441 20th St. NE

But in any case, streets and avenues are overwhelmingly numbered, not named, which kills your system completely. And the directional indicator is always capitalized, even when it is two letters.

Ah, I live in England so we don't have directional indicators for our streets, so hopefully that solves the issue :)

Old Pedant
03-19-2012, 10:43 PM
And you never have numbered streets, either??

123 25th Street

???

APD1993
03-19-2012, 10:58 PM
var nameRE = /^[A-Z]([a=z]|[\'\-][A-Z])*[a-z](\s[A-Z]([a=z]|[\'\-][A-Z])?$/;

You didn't ask for it, but...

That insists that the first and last name start with an upper case letter and otherwise consist of only lower case letters, EXCEPT that if ' or - is included the letter after the ' or - must also be upper case.

So:
John O'Brien works
Mallory Spence-Waite works
JD Powers does NOT work
Bob McFarlane does NOT work (oops! do we need to handle this, too?)
April White-O does NOT work (can't end with upper case letter)

If you don't care about being so fancy with upper/lower case:


var nameRE = /^[a-z][a=z\'\-]*[a-z](\s[a-z][a=z\'\-]*[a-z])?$/i; // i means ignore case


************


var addrRE = /^\d+(\s[A-Z][a-z\'\-\.]*[a-z\.])+$/

Again, I modified your requirements slightly. That allows ' or - or . anywhere in a "word" but only allows the period at the end of the word.

123 Fourth Ave. is okay
123 W. Whistle-stop is okay
777 Jone's Rd. is okay
Jackson And Pine is NOT okay (I think it should be)
Box 666 is NOT okay (I think it should be)
1 Saints' Street is NOT okay

You will have to decide if you want the rules to be that restrictive, in actuality.

For the address coding, I am trying something like this:



function address2(address)
{
var reg=/^\d+(\s[A-Z][a-z\'\-\.]*[a-z\.])+$/;
var x=document.forms["customer"]["compaddress"].value;
if (reg.test(x)===false)
{
alert("You have not entered a valid address in the Company Address field. Enter in a suitable value (i.e. in a 123 Real Street format)");
}
}


And calling it like this:



else if (address2(address)===false)
{
return address2(address);
return false;
}
}


However, when I try something such as Box 666, it doesn't display the test alert, suggesting that I have implemented it wrong :/

Is there any way to fix this? :)

Old Pedant
03-19-2012, 11:48 PM
Code makes no sense.

You pass a value to the function when you call it:


if (address2(address)===false)

but then in the function you never use the passed argument and *ALWAYS* go check

document.forms["customer"]["compaddress"].value;

So if the value in "compaddress" is okay, you indeed won't get any alert.

On top of that, you do

return address2(address);
return false;

The first return means the second one is unnecessary. return means "return from the function RIGHT NOW and do not execute ANY more code in the function."

So... I can't really tell why/how this is/isn't working without seeing the <form> in question. But in any case, either use the address argument to the function or don't have an argument, at all.

Philip M
03-19-2012, 11:56 PM
Try this:-


<script type = "text/javascript">

var str1 = "jEAn-paul o'flaNAGan-macDONald";
str = str.toLowerCase().replace(/\b[a-z]/g,function(w){return w.toUpperCase()});
alert(str);

</script>

My own house has a name, not a number. I don't know of any UK addresses like 123 56th Street, but it may be possible. Roads/streets/avenues are normally named. I would not impose too strict requirements on your users to comply with a particular format. They ought to know their own address. If you want the words capitalised, use the above script rather than reject an entry because it is not properly capitalised. That just annoys your users.

Here is a full validation for First Name and Last Name (easily changed if you want first and last names in a single field):-



<html>
<head>

<script type = "text/javascript">

function check(form) {
var fn = form.firstname;
var fnv = form.firstname.value;
fnv = fnv.replace(/\s+/g,""); // strip all spaces OR
//fnv = fnv.replace(/^\s+|\s+$/g,""); // strip only leading and trailing spaces;
//fnv = fnv.replace(/\s{2,}/g," "); // Replace multiple spaces with one space
// convert whatever is entered into lower case but with a capitalised first letter
fnv = fnv.toLowerCase().replace(/\b[a-z]/g,function(w){return w.toUpperCase()});
form.firstname.value = fnv; // write it back to the field
if ((fnv == "Firstname") || (fnv.length < 2) || (/[^a-z\-\']/gi.test(fnv))) { // only a-z hyphen and apostophe allowed in proper name
form.firstname.value= ""; // clear the invalid field
alert("Invalid characters or name too short! Please enter your first name!");
myfield = fn; // note myfield must be a global variable
setTimeout('myfield.focus();myfield.select();' , 10); // to overcome bug in Firefox
return false;
}

var ln = form.lastname;
var lnv = form.lastname.value;
lnv = lnv.replace(/\s+/g,""); // strip all spaces OR
//lnv = lnv.replace(/^\s+|\s+$/g,""); // strip only leading and trailing spaces;
//lnv = lnv.replace(/\s{2,}/g," "); // Replace multiple spaces with one space
// convert whatever is entered into lower case but with a capitalised first letter
lnv = lnv.toLowerCase().replace(/\b[a-z]/g,function(w){return w.toUpperCase()});
form.lastname.value = lnv; // write it back to the field
if ((lnv == "Lastname") || (lnv.length < 3) || (/[^a-z\-\']/gi.test(lnv))) { // only a-z hyphen and apostophe allowed in proper name
form.lastname.value= ""; // clear the invalid field
alert("Invalid characters or name too short! Please enter your last name!");
myfield = ln; // note myfield must be a global variable
setTimeout('myfield.focus();myfield.select();' , 10); // to overcome bug in Firefox
return false;
}

return true;
}

</script>
</head>
<body>

<form name = "form" action="" onsubmit="return check(this)">
<input id="firstname" name="firstname" type="text" value="First Name" onfocus="this.value==this.defaultValue?this.value='':null" onblur="this.value==''?this.value=this.defaultValue:null">
<input id="lastname" name="lastname" type="text" value="Last Name" onfocus="this.value==this.defaultValue?this.value='':null" onblur="this.value==''?this.value=this.defaultValue:null">
<input type="submit" value="Submit">
</form>

</body>
</html>

Old Pedant
03-20-2012, 12:04 AM
Philip: Wasn't it you who noted that some Scottish names need inner capitalization and some don't?

e.g., MacIntosh vs. Macadam? (I don't think those actual examples are correct, but I'm sure you can correct me.)

Philip M
03-20-2012, 12:21 AM
Philip: Wasn't it you who noted that some Scottish names need inner capitalization and some don't?

e.g., MacIntosh vs. Macadam? (I don't think those actual examples are correct, but I'm sure you can correct me.)

I understand that the prefix Mac meaning "son of" requires the following proper name to be capitalised - MacDonald, but where the prefix Mac is followed by a person's occupation, e.g. Macnab (son of the abbott), Maccosh (son of the footman), or Mackenzie (son of the fair one) the part after Mac is not capitalised.

http://www.codingforums.com/showthread.php?t=248812

If you want to cope with this, you must use one of the suggested scripts in that thread. Or perhaps best to just ignore it. MacDonald is in fact often spelled Macdonald. But the hamburger chain is of course McDonalds.

APD1993
03-20-2012, 12:22 AM
Code makes no sense.

You pass a value to the function when you call it:


if (address2(address)===false)

but then in the function you never use the passed argument and *ALWAYS* go check

document.forms["customer"]["compaddress"].value;

So if the value in "compaddress" is okay, you indeed won't get any alert.

On top of that, you do

return address2(address);
return false;

The first return means the second one is unnecessary. return means "return from the function RIGHT NOW and do not execute ANY more code in the function."

So... I can't really tell why/how this is/isn't working without seeing the <form> in question. But in any case, either use the address argument to the function or don't have an argument, at all.

My form for this is as follows:



<form name="customer" action="mailto:apd15@hotmail.co.uk" method="post" onsubmit="return ValidateCustomerDetails();">
<p><b><u>Customer Details Form</b></u></p>
Full Name:<input type="text" name="custname"><br>
Company Name:<input type="text" name="compname"><br>
Company Address:<textarea cols="30" rows="5" name="compaddress"></textarea><br>
Customer E-Mail Address:<input type="text" name="custemailaddress"><br>
Company Phone Number:<input type="text" name="compphonenumber"><br>
</form>


And my JavaScript function that I am trying to apply to it is the following:



function ValidateCustomers()
{
var name=document.forms["customer"]["custname"].value;
var comp=document.forms["customer"]["compname"].value;
var address=document.forms["customer"]["compaddress"].value;
var email=document.forms["customer"]["custemailaddress"].value;
var phone=document.forms["customer"]["compphonenumber"].value;
if (((((hasWhiteSpace(name))&&(hasWhiteSpace(comp))&&(hasWhiteSpace(address))&&(hasWhiteSpace2(email))&&(hasWhiteSpace(phone))))))
{
alert("You have not entered any suitable values for the Full Name, Company Name, Company Address, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if (((((name===null||name==="")&&(comp===null||comp==="")&&(address===null||address==="")&&(email===null||email==="")&&(phone===null||phone==="")))))
{
alert("You have not entered any suitable values for the Full Name, Company Name, Company Address, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values");
return false;
}
else if ((((hasWhiteSpace(name))&&(hasWhiteSpace(comp))&&(hasWhiteSpace(address))&&(hasWhiteSpace2(email)))))
{
alert("You have not entered any suitable values for the Full Name, Company Name, Company Address or Customer E-Mail Address fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if ((((name===null||name==="")&&(comp===null||comp==="")&&(address===null||address==="")&&(email===null||email===""))))
{
alert("You have not entered any suitable values for the Full Name, Company Name, Company Address or Customer E-Mail Address fields. Enter in suitable values");
return false;
}
else if ((((hasWhiteSpace(name))&&(hasWhiteSpace(comp))&&(hasWhiteSpace(address))&&(hasWhiteSpace(phone)))))
{
alert("You have not entered any suitable values for the Full Name, Company Name, Company Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if ((((name===null||name==="")&&(comp===null||comp==="")&&(address===null||address==="")&&(phone===null||phone===""))))
{
alert("You have not entered any suitable values for the Full Name, Company Name, Company Address or Company Phone Number fields. Enter in suitable values");
return false;
}
else if ((((hasWhiteSpace(name))&&(hasWhiteSpace(comp))&&(hasWhiteSpace2(email))&&(hasWhiteSpace(phone)))))
{
alert("You have not entered any suitable values for the Full Name, Company Name, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if ((((name===null||name==="")&&(comp===null||comp==="")&&(email===null||email==="")&&(phone===null||phone===""))))
{
alert("You have not entered any suitable values for the Full Name, Company Name, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values");
return false;
}
else if ((((hasWhiteSpace(name))&&(hasWhiteSpace(address))&&(hasWhiteSpace2(email))&&(hasWhiteSpace(phone)))))
{
alert("You have not entered any suitable values for the Full Name, Company Address, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if ((((name===null||name==="")&&(address===null||address==="")&&(email===null||email==="")&&(phone===null||phone===""))))
{
alert("You have not entered any suitable values for the Full Name, Company Address, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values");
return false;
}
else if ((((hasWhiteSpace(name))&&(hasWhiteSpace(address))&&(hasWhiteSpace2(email))&&(hasWhiteSpace(phone)))))
{
alert("You have not entered any suitable values for the Full Name, Company Address, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if ((((name===null||name==="")&&(address===null||address==="")&&(email===null||email==="")&&(phone===null||phone===""))))
{
alert("You have not entered any suitable values for the Full Name, Company Address, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values");
return false;
}
else if ((((hasWhiteSpace(name))&&(hasWhiteSpace(address))&&(hasWhiteSpace2(email))&&(hasWhiteSpace(phone)))))
{
alert("You have not entered any suitable values for the Full Name, Company Address, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if ((((name===null||name==="")&&(address===null||address==="")&&(email===null||email==="")&&(phone===null||phone===""))))
{
alert("You have not entered any suitable values for the Full Name, Company Address, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values");
return false;
}
else if ((((hasWhiteSpace(comp))&&(hasWhiteSpace(address))&&(hasWhiteSpace2(email))&&(hasWhiteSpace(phone)))))
{
alert("You have not entered any suitable values for the Company Name, Company Address, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if ((((comp===null||comp==="")&&(address===null||address==="")&&(email===null||email==="")&&(phone===null||phone===""))))
{
alert("You have not entered any suitable values for the Company Name, Company Address, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values");
return false;
}
else if (((hasWhiteSpace(name))&&(hasWhiteSpace(comp))&&(hasWhiteSpace(address))))
{
alert("You have not entered any suitable values for the Full Name, Company Name or Company Address fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if (((name===null||name==="")&&(comp===null||comp==="")&&(address===null||address==="")))
{
alert("You have not entered any suitable values for the Full Name, Company Name or Company Address fields. Enter in suitable values");
return false;
}
else if (((hasWhiteSpace(name))&&(hasWhiteSpace(comp))&&(hasWhiteSpace2(email))))
{
alert("You have not entered any suitable values for the Full Name, Company Name or Customer E-Mail Address fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if (((name===null||name==="")&&(comp===null||comp==="")&&(email===null||email==="")))
{
alert("You have not entered any suitable values for the Full Name, Company Name or Customer E-Mail Address fields. Enter in suitable values");
return false;
}
else if (((hasWhiteSpace(name))&&(hasWhiteSpace(comp))&&(hasWhiteSpace(phone))))
{
alert("You have not entered any suitable values for the Full Name, Company Name or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if (((name===null||name==="")&&(comp===null||comp==="")&&(phone===null||phone==="")))
{
alert("You have not entered any suitable values for the Full Name, Company Name or Company Phone Number fields. Enter in suitable values");
return false;
}
else if (((hasWhiteSpace(name))&&(hasWhiteSpace(address))&&(hasWhiteSpace2(email))))
{
alert("You have not entered any suitable values for the Full Name, Company Address or Customer E-Mail Address fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if (((name===null||name==="")&&(address===null||address==="")&&(email===null||email==="")))
{
alert("You have not entered any suitable values for the Full Name, Company Address or Customer E-Mail Address fields. Enter in suitable values");
return false;
}
else if (((hasWhiteSpace(name))&&(hasWhiteSpace(address))&&(hasWhiteSpace(phone))))
{
alert("You have not entered any suitable values for the Full Name, Company Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if (((name===null||name==="")&&(address===null||address==="")&&(phone===null||phone==="")))
{
alert("You have not entered any suitable values for the Full Name, Company Address or Company Phone Number fields. Enter in suitable values");
return false;
}
else if (((hasWhiteSpace(name))&&(hasWhiteSpace2(email))&&(hasWhiteSpace(phone))))
{
alert("You have not entered any suitable values for the Full Name, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if (((name===null||name==="")&&(email===null||email==="")&&(phone===null||phone==="")))
{
alert("You have not entered any suitable values for the Full Name, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values");
return false;
}
else if (((hasWhiteSpace(comp))&&(hasWhiteSpace(address))&&(hasWhiteSpace2(email))))
{
alert("You have not entered any suitable values for the Company Name, Company Address or Customer E-Mail Address fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if (((comp===null||comp==="")&&(address===null||address==="")&&(email===null||email==="")))
{
alert("You have not entered any suitable values for the Company Name, Company Address or Customer E-Mail Address fields. Enter in suitable values");
return false;
}
else if (((hasWhiteSpace(comp))&&(hasWhiteSpace2(email))&&(hasWhiteSpace(phone))))
{
alert("You have not entered any suitable values for the Company Name, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if (((comp===null||comp==="")&&(email===null||email==="")&&(phone===null||phone==="")))
{
alert("You have not entered any suitable values for the Company Name, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values");
return false;
}
else if (((hasWhiteSpace(address))&&(hasWhiteSpace2(email))&&(hasWhiteSpace2(phone))))
{
alert("You have not entered any suitable values for the Company Address, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if (((comp===null||comp==="")&&(address===null||address==="")&&(email===null||email==="")))
{
alert("You have not entered any suitable values for the Company Name, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values");
return false;
}
else if ((hasWhiteSpace(name))&&(hasWhiteSpace(comp)))
{
alert("You have not entered any suitable values for the Full Name or Company Name fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if ((name===null||name==="")&&(comp===null||comp===""))
{
alert("You have not entered any suitable values for the Full Name or Company Name fields. Enter in suitable values");
return false;
}
else if ((hasWhiteSpace(name))&&(hasWhiteSpace(address)))
{
alert("You have not entered any suitable values for the Full Name or Company Address fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if ((name===null||name==="")&&(address===null||address===""))
{
alert("You have not entered any suitable values for the Full Name or Company Address fields. Enter in suitable values");
return false;
}
else if ((hasWhiteSpace(name))&&(hasWhiteSpace2(email)))
{
alert("You have not entered any suitable values for the Full Name or Customer E-Mail Address fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if ((name===null||name==="")&&(email===null||email===""))
{
alert("You have not entered any suitable values for the Full Name or Customer E-Mail Address fields. Enter in suitable values");
return false;
}
else if ((hasWhiteSpace(name))&&(hasWhiteSpace(phone)))
{
alert("You have not entered any suitable values for the Full Name or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if ((name===null||name==="")&&(phone===null||phone===""))
{
alert("You have not entered any suitable values for the Full Name or Company Phone Number fields. Enter in suitable values");
return false;
}
else if ((hasWhiteSpace(comp))&&(hasWhiteSpace(address)))
{
alert("You have not entered any suitable values for the Company Name or Company Address fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if ((comp===null||comp==="")&&(address===null||address===""))
{
alert("You have not entered any suitable values for the Company Name or Company Address fields. Enter in suitable values");
return false;
}
else if ((hasWhiteSpace(comp))&&(hasWhiteSpace2(email)))
{
alert("You have not entered any suitable values for the Company Name or Customer E-Mail Address fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if ((comp===null||comp==="")&&(email===null||email===""))
{
alert("You have not entered any suitable values for the Company Name or Customer E-Mail Address fields. Enter in suitable values");
return false;
}
else if ((hasWhiteSpace(comp))&&(hasWhiteSpace(phone)))
{
alert("You have not entered any suitable values for the Company Name or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if ((comp===null||comp==="")&&(phone===null||phone===""))
{
alert("You have not entered any suitable values for the Company Name or Company Phone Number fields. Enter in suitable values");
return false;
}
else if ((hasWhiteSpace(address))&&(hasWhiteSpace2(email)))
{
alert("You have not entered any suitable values for the Company Address or Customer E-Mail Address fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if ((address===null||address==="")&&(email===null||email===""))
{
alert("You have not entered any suitable values for the Company Address or Customer E-Mail Address fields. Enter in suitable values");
return false;
}
else if ((hasWhiteSpace(address))&&(hasWhiteSpace(phone)))
{
alert("You have not entered any suitable values for the Company Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if ((address===null||address==="")&&(phone===null||phone===""))
{
alert("You have not entered any suitable values for the Company Address or Company Phone Number fields. Enter in suitable values");
return false;
}
else if ((hasWhiteSpace2(email))&&(hasWhiteSpace(phone)))
{
alert("You have not entered any suitable values for the Customer E-Mail Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if ((email===null||email==="")&&(phone===null||phone===""))
{
alert("You have not entered any suitable values for the Customer E-Mail Address or Company Phone Number fields. Enter in suitable values");
return false;
}
else if (hasWhiteSpace(name))
{
alert("You have not entered a suitable value for the Full Name field. Enter in a suitable value, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if (name===null||name==="")
{
alert("You have not entered a suitable value for the Full Name field. Enter in a suitable value");
return false;
}
else if (hasWhiteSpace(comp))
{
alert("You have not entered a suitable value for the Company Name field. Enter in a suitable value, possibly by removing any leading or trailing spaces around the text");
return false;
}
else if (comp===null||comp==="")
{
alert("You have not entered a suitable value for the Company Name field. Enter in a suitable value");
return false;
}
etc
else if (email2(email)===false)
{
return email2(email);
return false;
}
else if (phone2(phone)===false)
{
return phone2(phone);
return false;
}
else if (name2(name)===false)
{
return name2(name);
return false;
}
else if (address2(address)===false)
{
return address2(address);
return false;
}
}


It's essentially a lot of different branches for validation

Philip M
03-20-2012, 12:43 AM
The repetition should alert you to the fact that your code is very poorly written. I would advise that you validate each field separately (one at a time), with error messages relevant to that specific field and that specific error. You should strip leading and trailing spaces (and multiple consecutive spaces) in your script automatically as a matter of course, not complain about them to the user. The value of a form field can never be null, only "" (blank). Why are you using the === strict comparison operator?

Form validation of the pattern if (document.forms[0].elements[1].value == "") is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.

APD1993
03-20-2012, 12:45 AM
The repetition should alert you to the fact that your code is poorly written. I would advise that you validate each field separately (one at a time), with error messages relevant to that field and that specific error.

Form validation of the pattern if (document.forms[0].elements[1].value == "") is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.

I'm still struggling to see why the coding you gave me doesn't seem to work :/

I'm not going to have any validation for the company name, since companies can have strange names

Philip M
03-20-2012, 01:02 AM
All the code that I post works for me. :)

I notice that your form action is - action="mailto:apd15@hotmail.co.uk" :( This is a complete no-no.

The trouble with using this long-obsolete method (mailto) to send form results is its unpredictability. The method it is highly dependent on the browser in use and the email client in use (some people have only Yahoo, Gmail or Hotmail). In particular, your visitor must have Outlook or Outlook Express as the default client for this to work correctly. Even if your visitor is using Internet Explorer, but the default mail client is different (e.g. Eudora or Thunderbird), your mailto form will not work. With all of the browser troubles, you're likely to lose about half of your users' messages. Most of the email clients that can successfully send a mail will prompt the user with a somewhat threatening security dialog prior to sending - this can scare many users from continuing. Other users will not wish to reveal their email address. Also, what about people with Javascript disabled?

In addition, if you place an unobfuscated email address in your webpage, the bots will quickly find it and inundate you in spam.

Modern browsers no longer accept mailto: as a form action - they simply open the email program (if any) and ignore the form. If you are going to use a form then use a server-side CGI formmail script as the action - there are several good free ones out there.

I do not wish to be thought unkind, but it seems to me that you simply do not know enough about Javascript to do what you intend. The fault, dear Brutus, lies not in the stars ...

Old Pedant
03-20-2012, 01:04 AM
Oh my lord, what a MESS!

First of all, the .value property of a <form> field is *NEVER* null. So all your ==null checks are useless. (The .value property may of course be a blank string "", but that is *NOT* a null.)

And if you use a regular expression to validate, you don't need to make the silly hasWhiteSpace checks.

And if you check each field separately and BUILD UP the error message, then you don't need all that complex set of multiple if conditions.

But having said all that, the biggest problem you face is here:

<form ... action="mailto:apd15@hotmail.co.uk" ...>

Roughly a third to a half of all typical users will be unable to use your <form>. "mailto" only works if the user's browser is properly set up to indeed send <form> data to an email program. Which means that a user using a browser other than the default one won't be set up properly unless he/she understands the often arcane ways to specify an email client to non-default (e.g, in most cases non-IE) browsers.

You *REALLY* should instead be sending the <form> to some server-side code (PHP, CGI, ASP, etc.) that will take the form <post> and send the email for you, so that you aren't depending upon the user's ability to link his/her browser to the email client.

APD1993
03-20-2012, 01:06 AM
I notice that your form action is - action="mailto:apd15@hotmail.co.uk" :( This is a complete no-no.

The trouble with using this long-obsolete method (mailto) to send form results is its unpredictability. The method it is highly dependent on the browser in use and the email client in use (some people have only Yahoo, Gmail or Hotmail). In particular, your visitor must have Outlook or Outlook Express as the default client for this to work correctly. Even if your visitor is using Internet Explorer, but the default mail client is different (e.g. Eudora or Thunderbird), your mailto form will not work. With all of the browser troubles, you're likely to lose about half of your users' messages. Most of the email clients that can successfully send a mail will prompt the user with a somewhat threatening security dialog prior to sending - this can scare many users from continuing. Other users will not wish to reveal their email address. Also, what about people with Javascript disabled?

In addition, if you place an unobfuscated email address in your webpage, the bots will quickly find it and inundate you in spam.

Modern browsers no longer accept mailto: as a form action - they simply open the email program (if any) and ignore the form. If you are going to use a form then use a server-side CGI formmail script as the action - there are several good free ones out there.

I'm currently focusing on the client side aspect of things, but thanks for the heads up! :)

Shame I'm struggling with the name and address validation though :/

Old Pedant
03-20-2012, 01:07 AM
Well, trust Philip to be faster than I am! LOL! But you can see we agree on this.

By the by, Philip, I discovered that indeed I can use mailto: with Chrome. But only if I know how to tell Chrome to link to the email client. I'd bet that fewer than 10% of all typical Chrome users will figure it out.

Old Pedant
03-20-2012, 01:07 AM
Shame I'm struggling with the name and address validation though :/

Give me a bit. I'll play with it all.

APD1993
03-20-2012, 01:12 AM
Give me a bit. I'll play with it all.

I can't thank you enough for this :D

Old Pedant
03-20-2012, 01:36 AM
Here, this seems to work:


<html>
<head>
<script type="text/javascript">
function trim(str)
{
return str.replace(/^\s+/,"").replace(/\s+$/,"");
}

function ValidateCustomerDetails(form)
{
var name = trim(form.custname.value);
var comp = trim(form.compname.value);
var address = trim(form.compaddress.value);
var email = trim(form.custemailaddress.value);
var phone = trim(form.compphonenumber.value);

var oops = "";

var addrRE = /^\d+(\s[A-Z][a-z\'\-\.]*[a-z\.])+$/;
var nameRE = /^[a-z][a-z\'\-]*[a-z](\s[a-z][a-z\'\-]*[a-z])?$/i;
var emailRE = /^([\w\'\-]+\.)*[\w\'\-]+\@([\w\'\-]+\.)+[a-z]{2,6}$/i;
var phoneRE = /^\d{6,12}$/; /* allows 6 through 12 digits for phone...adjust as needed */
var zapRE = /[\s\-\(\)\]/g; /* removes space, dashes, and parentheses */

if ( ! nameRE.test(name) )
{
oops += "\n You must supply a vaild full name.";
}
if ( comp.replace(zapRE,"").length < 3 ) /* just insists on at least 3 characters in company name */
{
oops += "\n You must supply a vaild company name.";
}
if ( ! addrRE.test(address) )
{
oops += "\n You must supply a vaild address.";
}
if ( ! emailRE.test(email) )
{
oops += "\n You must supply a vaild email address.";
}
if ( ! phoneRE.test(phone.replace(zapRE,"")) )
{
oops += "\n You must supply a vaild phone number.";
}
if ( oops != "" )
{
alert("Please correct the following error(s):" + oops);
return false;
}
/* ********** FOR TESTING ONLY *********** */
alert("Passed validation");
return false;
/* ********** REMOVE FOR PRODUCTION ********* */

return true;
}
</script>
</head>
<body>
<form name="customer" action="mailto:apd15@hotmail.co.uk"
method="post" onsubmit="return ValidateCustomerDetails(this);">

<p><b><u>Customer Details Form</b></u></p>

Full Name:<input type="text" name="custname"/><br/>

Company Name:<input type="text" name="compname"/><br/>

Company Address:<textarea cols="30" rows="5" name="compaddress"/></textarea><br/>

Customer E-Mail Address:<input type="text" name="custemailaddress"/><br/>

Company Phone Number:<input type="text" name="compphonenumber"/><br/>

<input type="submit" value="Send Mail" />
</form>
</body>
</html>

I don't understand why you want to use a <textarea> for the address when the regular expression you are using is so limiting on what an address can be. And I agree with Philip that your rules for the address seem WAY overly restrictive. Note that he lives in the UK and, as he points out, he has NO number as part of his address.

But what the heck...

Old Pedant
03-20-2012, 01:44 AM
By the way, what about people with three parts to their full name? "Major Mark Smith"?

I *REALLY* feel you should WAY loosen up the restrictions you placed on name and address. Why does it matter, for example, if the person enters a middle initial? "John Q. Public"?? Just for example.

APD1993
03-20-2012, 01:49 AM
Here, this seems to work:


<html>
<head>
<script type="text/javascript">
function trim(str)
{
return str.replace(/^\s+/,"").replace(/\s+$/,"");
}

function ValidateCustomerDetails(form)
{
var name = trim(form.custname.value);
var comp = trim(form.compname.value);
var address = trim(form.compaddress.value);
var email = trim(form.custemailaddress.value);
var phone = trim(form.compphonenumber.value);

var oops = "";

var addrRE = /^\d+(\s[A-Z][a-z\'\-\.]*[a-z\.])+$/;
var nameRE = /^[a-z][a-z\'\-]*[a-z](\s[a-z][a-z\'\-]*[a-z])?$/i;
var emailRE = /^([\w\'\-]+\.)*[\w\'\-]+\@([\w\'\-]+\.)+[a-z]{2,6}$/i;
var phoneRE = /^\d{6,12}$/; /* allows 6 through 12 digits for phone...adjust as needed */
var zapRE = /[\s\-\(\)\]/g; /* removes space, dashes, and parentheses */

if ( ! nameRE.test(name) )
{
oops += "\n You must supply a vaild full name.";
}
if ( comp.replace(zapRE,"").length < 3 ) /* just insists on at least 3 characters in company name */
{
oops += "\n You must supply a vaild company name.";
}
if ( ! addrRE.test(address) )
{
oops += "\n You must supply a vaild address.";
}
if ( ! emailRE.test(email) )
{
oops += "\n You must supply a vaild email address.";
}
if ( ! phoneRE.test(phone.replace(zapRE,"")) )
{
oops += "\n You must supply a vaild phone number.";
}
if ( oops != "" )
{
alert("Please correct the following error(s):" + oops);
return false;
}
/* ********** FOR TESTING ONLY *********** */
alert("Passed validation");
return false;
/* ********** REMOVE FOR PRODUCTION ********* */

return true;
}
</script>
</head>
<body>
<form name="customer" action="mailto:apd15@hotmail.co.uk"
method="post" onsubmit="return ValidateCustomerDetails(this);">

<p><b><u>Customer Details Form</b></u></p>

Full Name:<input type="text" name="custname"/><br/>

Company Name:<input type="text" name="compname"/><br/>

Company Address:<textarea cols="30" rows="5" name="compaddress"/></textarea><br/>

Customer E-Mail Address:<input type="text" name="custemailaddress"/><br/>

Company Phone Number:<input type="text" name="compphonenumber"/><br/>

<input type="submit" value="Send Mail" />
</form>
</body>
</html>

I don't understand why you want to use a <textarea> for the address when the regular expression you are using is so limiting on what an address can be. And I agree with Philip that your rules for the address seem WAY overly restrictive. Note that he lives in the UK and, as he points out, he has NO number as part of his address.

But what the heck...

Thank you so much :D

I'll give this a try tomorrow :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum