javascript - Regular expression for LastName, FirstName with space - Stack Overflow

I am using the below JS to enter name in LastName, FirstName format like this Clark, Michael and now I

I am using the below JS to enter name in LastName, FirstName format like this Clark, Michael and now I also probably need to allow names like this Tim Duncan, Vince Carter where these kinda names have space in between.

function validateName(txtbox) {
        var name = /^\w{1,20}\, \w{1,20}$/
        var check = document.getElementById(txtbox);
        if (check != null) {
            if (check.value != "") {
                if (!name.test(check.value)) {
                    alert('Please enter name in Last Name, First Name format');
                    document.getElementById(txtbox).focus();
                    return false;
                }
                else {
                    return true;
                }
            }
            else if (name.test(check.value)) {
                return true;
            }
            else if (check.value == "") {
                return true;
            }
        }
    }

Is there any way to achieve this by a change within the same Regex. Really appreciate suggestions and help.

I am using the below JS to enter name in LastName, FirstName format like this Clark, Michael and now I also probably need to allow names like this Tim Duncan, Vince Carter where these kinda names have space in between.

function validateName(txtbox) {
        var name = /^\w{1,20}\, \w{1,20}$/
        var check = document.getElementById(txtbox);
        if (check != null) {
            if (check.value != "") {
                if (!name.test(check.value)) {
                    alert('Please enter name in Last Name, First Name format');
                    document.getElementById(txtbox).focus();
                    return false;
                }
                else {
                    return true;
                }
            }
            else if (name.test(check.value)) {
                return true;
            }
            else if (check.value == "") {
                return true;
            }
        }
    }

Is there any way to achieve this by a change within the same Regex. Really appreciate suggestions and help.

Share Improve this question edited Jan 7, 2015 at 10:19 Toto 91.6k63 gold badges95 silver badges132 bronze badges asked Jan 7, 2015 at 9:51 MichaelMichael 3051 gold badge5 silver badges19 bronze badges 3
  • 1 20 characters max? Data processed by a Sinclair ZX81? ;-) – Serge Wautier Commented Jan 7, 2015 at 9:57
  • What about O'connors or Jean-François? – Toto Commented Jan 7, 2015 at 10:05
  • @M42 - I'm looking for a solution for that case and pls help if you could. – Michael Commented Jan 7, 2015 at 10:15
Add a ment  | 

3 Answers 3

Reset to default 4
^[\w ]{1,20}\, [\w ]{1,20}$

This should work for you.

Maybe this will meet your expectations.

/^\s*(\w{1,20} *[^,]*)+,\s+(\w{1,20}\s*)+$/

However above regex will accept also multiple white spaces before and after name. If you want to force only one space character format you should use these regex:

/^(\w{1,20} ?[^,]*)+, (\w{1,20}( [^$]|$))+$/

You can use html5 pattern..

<input type="text" title="Following format: FirstName LastName" pattern="^\s*([\w]{1,20}( |,)?)+\s*,\s+([\w]{1,20} *)+$" />

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744285501a4566777.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信