0

I am a complete beginner to javascript and I have several things I need to correct on a form in order for it to work. I have to make sure it doesn't reject any valid names (names with accents, hyphens, names with spaces between them). At the moment my regular expression is -

var alphabetic = /^[a-zA-Z]+$/;

if ((alphabetic.test(fname)== false) || (alphabetic.test(lname)== false))
{
    alertmsg = alertmsg + "Name should be in alphabets:" + "\n";
}

If someone could point me in the right direction, I would be very grateful

Matt Burland
  • 44,552
  • 18
  • 99
  • 171

2 Answers2

1

Try this regex :

var alphabetic = /^[a-zàâçéèêëîïôûùüÿñ-\s]+$/i
Philippe Boissonneault
  • 3,949
  • 3
  • 26
  • 33
  • ok thank you everyone, so if wanted to have a space in the postcode also separating the two parts? var postcode = /^(A-Z){1,2}\d(A-Z\d)?\d(A-Z){2}$/ – user2073133 Feb 14 '13 at 18:30
  • postal code from where ? http://geekswithblogs.net/MainaD/archive/2007/12/03/117321.aspx – Philippe Boissonneault Feb 14 '13 at 18:35
  • This seems to do the trick http://stackoverflow.com/questions/164979/uk-postcode-regex-comprehensive – Philippe Boissonneault Feb 14 '13 at 18:40
  • I have only allowed for upper case letters in the postcodes with the above expression, which wasn't desirable either. – user2073133 Feb 14 '13 at 18:42
  • No problem, you can add /REGEX/i modifier to use case insensitive – Philippe Boissonneault Feb 14 '13 at 18:47
  • If I needed to allow for a space in a UK telephone, how would I do that? var telephone = /^0\d{9,10}$/ – user2073133 Feb 14 '13 at 18:47
  • this could help you http://www.w3schools.com/jsref/jsref_obj_regexp.asp space is \s – Philippe Boissonneault Feb 14 '13 at 18:48
  • thank you Philippe, one last question if you don't mind. To validate a statement I have made sure that the checkboxes and quantities are consistent with each other but I haven't made sure that at least one product has been selected or a total quantity greater than zero is supplied. How would I do this? – user2073133 Feb 14 '13 at 19:07
  • if (((document.form1.ch1.checked) && (summer2012 <=0)) || ((document.form1.autumn.checked) && (autumn2012 <=0)) || ((document.form1.winter.checked) && (winter2012 <=0))) { alertmsg = alertmsg + "Please enter Quantity" +"\n"; } else if (((!document.form1.summer.checked) && (summer2012 >0)) || ((!document.form1.autumn.checked) && (autumn2012 >0)) || ((!document.form1.winter.checked) && (winter2012 >0))) { alertmsg = alertmsg + "Please choose Product" +"\n"; } – user2073133 Feb 14 '13 at 19:07
  • I'm not sure to understand what you need, your js script seems ok, perhaps you'll need to open another stackoverflow question – Philippe Boissonneault Feb 14 '13 at 19:19
1

As Philippe recommended, if you would like to accept languages/alphabets other than English, I would consider more carefully which letters to include. [a-zA-Z] does not seem to recognize letters other than strictly 'A' to 'Z' in my testing.