1

I have a question for you that I can't seem to figure out on my own.
Let's say that I want to validate a users first name. Some can contain multiple parts like "John William" with a space in between. What I want to do is match the input to a regular expression that seeks out if the name contains any spaces at the beginning, or at the end.
Further more the regular expression should check if there are ONLY letters (a-z, both lower and upper case) in the name.

This is what I came up with so far:

/^\s+[A-z]+|[A-z]+\s+$/

But somehow this regular expression does not take any other characters (such as dash, underscore, ampersand, etc.) into notice. Basically all it does is tell me wether there are spaces at the beginning or at the end of the input.

Can anyone help me out here?

EDIT:

Here's the full code i'm using:

$(document).ready(function() {
    $('#firstname, #lastname').bind('keyup blur', function() {
        var _input = $(this);
        var _illegal = Validate_Regexp(_input.val(), /^\s+[A-Za-z]+|[A-Za-z]+\s+$/);
        if (_illegal == true) {
            $("#"+_input.attr('id')+".validator").css({
                'background-image' : 'url(./images/icons/bullet_red.png)',
            });
        } else {
            $("#"+_input.attr('id')+".validator").css({
                'background-image' : 'url(./images/icons/bullet_green.png)',
            });
        }
    });
});

function Validate_Regexp($value, $regexp) {
    return $regexp.test($value);
}

EDIT 2:

I'm going with Charlie's answer, however his answer forced me to have 2 parts of the name, instead of as much as I'd like.
I changed the code from:

var isLegal = /^[a-zA-Z]+(\s+[a-zA-Z]+)?$/.test(stringToTest);

to:

var isLegal = /^[a-zA-Z]+(\s[a-zA-Z]+)*?$/.test(stringToTest);
3
  • To check for upper and lower letters, use [a-zA-Z] Commented Jul 29, 2012 at 23:50
  • @kontur what [A-z] does is basically the same as [a-zA-Z]. But this still didn't help my result. As a matter of fact, it only made it worse in such a way that the input will only be checked for spaces at the end if there is actually a second part after a space in the middle. Commented Jul 29, 2012 at 23:54
  • I didn't know that, just never seen that writing form before. Even on developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions there seems to usage of a-z and A-Z explicitly - maybe for mere clarity. Commented Jul 29, 2012 at 23:59

2 Answers 2

2

I noticed that you are checking for strings that are illegal. Let's turn it around and check for a string that is valid:

 var isValid = /^[a-zA-Z]+(\s+[a-zA-Z]+)?$/.test(stringToTest);

Results:

 /^[a-zA-Z]+(\s+[a-zA-Z]+)?$/.test("john doe"); // true
 /^[a-zA-Z]+(\s+[a-zA-Z]+)?$/.test("john"); // true

 /^[a-zA-Z]+(\s+[a-zA-Z]+)?$/.test("john d_oe"); // false
 /^[a-zA-Z]+(\s+[a-zA-Z]+)?$/.test(" john doe "); // false
 /^[a-zA-Z]+(\s+[a-zA-Z]+)?$/.test(" john doe"); // false
 /^[a-zA-Z]+(\s+[a-zA-Z]+)?$/.test("john doe "); // false
 /^[a-zA-Z]+(\s+[a-zA-Z]+)?$/.test("john "); // false

Translated to your existing code:

    var isValid = Validate_Regexp(_input.val(), /^[a-zA-Z]+(\s+[a-zA-Z]+)?$/);
    if (isValid) {
        $("#"+_input.attr('id')+".validator").css({
            'background-image' : 'url(./images/icons/bullet_blue.png)',
        });
    } else {
        $("#"+_input.attr('id')+".validator").css({
            'background-image' : 'url(./images/icons/bullet_red.png)',
        });
    }
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your reply. I can't see how I missed that, because it's indeed very crucial for my result, however, it still allows characters like dash, underscore, ampersand, etc, to occur.
To be honest, it still doesn't. I've updated my question with the full code I'm using, maybe that will shed a new light on the subject.
I noticed that you are checking for strings that are illegal. Let's turn it around and check for a string that is valid. (You don't want any spaces to surround the name, right?)
You're right, I don't want any spaces surrounding the name. But I do want people to be able to use one name instead of forcing them to use two names.
Ah. Se updated answer. This should work /^[a-zA-Z]+(\s[a-zA-Z]+)?$/
0

You really don't care if there are leading or trailing spaces, or how many there are in between the names, all that stuff is very easy to manage without bothering the user.

So a suitable function might be:

function checkNames(s) {
  // Remove excess spaces
  s.replace(/(^\s*)|(\s*$)/g, '').replace(/\s+/g, ' ');

  // Check remaining content
  return /[a-z]+ [a-z]+/i.test(s);
}

But note that names can be hyphenated and contain numbers (e.g. William Gates the 3rd) or letters other than those in the English alphabet. So usually you let users type whatever they want and just deal with the spaces.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.