Using Regular Expression

Hi,

I have been looking online today and found a lot of examples about using regular expressions but I can’t get any of them to work.

I wanted to try creating a basic expression first. I want someone to enter a string that is 2 - 20 characters long and only has letters.

This is what I use so far, and it doesn’t work, nothing happens at all when I run it.


// Check for a valid name.
var namePattern = new RegExp("/^[a-zA-Z]{2,20}$/");
if (namePattern.test(name))
{
	window.alert("Invalid");
}

name comes from a input box I display on screen.

Also, I would like to update the expression so that there can be one space in the string, but it cannot be at the beginning or end. So this string is like a persons name.

Bob is OK,
Bob Smith is also OK.

Edit: Actually, it is doing something but its always coming back invalid.

MrGeeker, what would be good is to take a list of random names from Facebook, and choose obscure ones. Test your regexp against those, and test the rest of the class’s against them as well.

There are more characters that exist in legal names than there are that don’t. If I wanted, I could change my name LEGALLY to <. That would be 100% valid and legal.

The only validation you should use on names, imo, is the string length (after being made safe) to make sure it fits in your database.

That’s in the English language. And what about the people who change their name by deed poll.

Do you think Facebook validates names? I very much doubt it, and they are the most international community website on the net

Good luck MrGeeker, it’s very powerful stuff. Took me a while just to get the hang of the basics, but once you’ve got it it’ll save you loads of lines of code :slight_smile:

As you can make the string safe to use in the rest of your script there is no reason to tell people that their name is invalid.

Yes, but this project is for college and I have to do it the way the professor wants it done, even though it may not be great for a real application. Its suppose to validate to letters only. Its testing how to validate user input is all. Most people are doing it with a simple instr function.

But he talked about regular expressions the other day and I wanted to try it out.

He gave us a link to this website and told us we can register here and ask for help on these forums, but we have to post a link to the thread in our project code so he can see what was done.

Thought I would let you know…

Validating names is not good practise. You will be telling someone that their name O’riley is invalid. There are also so many other variations of names, some with hyphens, some with characters, single letter names.

What is thought of as good practise is having no validation of names, but just proper escaping or character replacement. You will then not be telling someone that their name is invalid, or limiting people by their place of origin :slight_smile:

Validating ALL fields is good practice. It is just a matter of determining appropriate validation. Obviously there are a few characters other than just letters that are valid in names.

You just left out the ! in the if statement.

Amending to allow spaces anywhere other than the start or finish is fairly easy. Testing to make sure there is only one space is more easily done by performing two tests.

var namePattern = new RegExp("/^[a-zA-Z][a-zA-Z ]{,18}[a-zA-Z]$/");
var namePattern2 = new RegExp("/^[a-zA-Z]+ *[a-zA-Z]+$/");
if (!namePattern.test(name) || !namePattern2.test(name))
{
	window.alert("Invalid");
}

It’s not that it’s picky, you got it the wrong way round. Originally it should have been:

// Check for a valid name.
var namePattern = new RegExp("/^[a-zA-Z]{2,20}$/");
if (!namePattern.test(name))
{
	window.alert("Invalid");
}

Note the !.

But I’m still not sure about modifying the regex to allow one space in the string as long as its not the first or last character. I can’t find any examples of that.

Unfortunately demanding that only one space is allowed makes it hard to limit the length. I would just use this simple expression and check the length of “name”:

if (name.length > 1 && name.length < 21 && /^([a-zA-Z]+ ?[a-zA-Z]+)$/.test(name)) {
  // ok
}
else {
  // invalid
}

Note that you don’t need to store the regular expression in a variable (unless you intend to use it again, in which case it’s a good idea).

Never mind I found the error.


if (namePattern.test(name) == false)
{
	window.alert("Invalid");
}

Wow, what a picky syntax, that would have worked in PHP or C# or Java.


But I’m still not sure about modifying the regex to allow one space in the string as long as its not the first or last character. I can’t find any examples of that.

I originally had the ! operator in there, but it still wasn’t working so I removed it to test something else.

I’m not sure what was wrong but that original code didn’t work at all for me. Also, if I remove the “== false” from the statement it stops working again.

I don’t know what causes this.