27

I want to enforce that

  1. the input firstname should only contains characters A-Z, a-z, and -
  2. the input login name should only contains alphanumeric characters

How do I restrict the two rules in javascript?

Below is my code (jsp) for username regex. But it's not working properly.

function validateForm(){
    var nameRegex = /^[a-zA-Z\-]+$/;
    var validfirstUsername = document.frm.firstName.value.match(nameRegex);
    if(validUsername == null){
        alert("Your first name is not valid. Only characters A-Z, a-z and '-' are  acceptable.");
        document.frm.firstName.focus();
        return false;
    }
}

Thanks!

7
  • 1
    What's the difference between username and login? Furthermore, what's wrong with what you have (other than inconsistent naming)? Commented Mar 9, 2012 at 4:21
  • @JasonMcCreary One is firstname and the other is login name. Using above code, if I use horatio2m as firstname input, the user can still be saved, which is not what I want. I want to validate that the firstname input should only contains a-z,A-Z,and - Commented Mar 9, 2012 at 4:26
  • 2
    Replace if(validUsername == null){ with if(validfirstUsername == null){. Commented Mar 9, 2012 at 4:28
  • @JoshPurvis Yes, you're right. A stupid mistake. Thank you. Commented Mar 9, 2012 at 4:34
  • 5
    The real problem here is that a user can simply turn off javascript and submit whatever username they want. please validate this on the backend, too. Commented Mar 9, 2012 at 21:43

3 Answers 3

36

The code you have looks fine, aside from the inconsistent variable reference (see the comment by Josh Purvis).

The following regex is fine for your first name spec:

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

Adding digits for your username check is straightforward:

var usernameRegex = /^[a-zA-Z0-9]+$/;

Note: There are many ways to write regular expressions. I've chosen to provide a version that matches what you started with. I encourage you to work through this Regular Expression Tutorial

Sign up to request clarification or add additional context in comments.

4 Comments

Can I use /[a-zA-Z\-]/ instead of /^[a-zA-Z\-]+$/ ? Why we need to use ^ in front of [a-zA-Z\-]?
^ means Matches the starting position within the string
^ means the beginning of the string. $ means the end. If you left off the ^, for example, ~@*##@horatio would pass your validation. Likewise for the $ and the end of the string being checked.
I would like to point out that this allows 12345 to pass as a valid username. How would you make sure there's at least one letter?
10

Here's the validation function I came up with, tailor it for your own use-cases:

function isUserNameValid(username) {
  /* 
    Usernames can only have: 
    - Lowercase Letters (a-z) 
    - Numbers (0-9)
    - Dots (.)
    - Underscores (_)
  */
  const res = /^[a-z0-9_\.]+$/.exec(username);
  const valid = !!res;
  return valid;
}

Comments

9

Usernames can only use letters, numbers, underscores, and periods.

  const onValidUsername = (val) => {
    const usernameRegex = /^[a-z0-9_.]+$/
    return usernameRegex.test(val)
  }

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.