Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript validation regex for names

I am looking to accept names in my app with letters and hyphens or dashes, i based my code on an answer i found here and coded that:

function validName(n){
  var nameRegex = /^[a-zA-Z\-]+$/;
  if(n.match(nameRegex) == null){
    return "Wrong";
  }
  else{
    return "Right";
  }
}

the only problem is that it accepts hyphen as the first letter (even multiple ones) which i don't want. thanks

like image 994
Joe D Avatar asked Feb 11 '17 09:02

Joe D


People also ask

How do you validate a name field in JavaScript?

You can use javascript test() method to validate name field. The test() method tests for a match in a string.

How to validate only for alphabets in JavaScript?

You can write a JavaScript form validation script to check whether the required field(s) in the HTML form contains only letters. To get a string contains only letters (both uppercase or lowercase) we use a regular expression (/^[A-Za-z]+$/) which allows only letters.

How do you write names in regex?

String regularExpression= "^[A-Za-z][A-Za-z0-9_]{7,29}$"; A valid username should start with an alphabet so, [A-Za-z]. All other characters can be alphabets, numbers or an underscore so, [A-Za-z0-9_]. Since length constraint was given as 8-30 and we had already fixed the first character, so we give {7,29}.

How to check string validation in JavaScript?

checkValidity() method is used to check if a string in <input> element's value attribute is URL . The checkvalidity() method returns true if the value is a proper URL and false if the input is not a proper URL.


1 Answers

Use negative lookahead assertion to avoid matching the string starting with a hyphen. Although there is no need to escape - in the character class when provided at the end of character class. Use - removed character class for avoiding - at ending or use lookahead assertion.

var nameRegex = /^(?!-)[a-zA-Z-]*[a-zA-Z]$/;
// or
var nameRegex = /^(?!-)(?!.*-$)[a-zA-Z-]+$/;

var nameRegex = /^(?!-)[a-zA-Z-]*[a-zA-Z]$/;
// or
var nameRegex1 = /^(?!-)(?!.*-$)[a-zA-Z-]+$/;

function validName(n) {
  if (n.match(nameRegex) == null) {
    return "Wrong";
  } else {
    return "Right";
  }
}

function validName1(n) {
  if (n.match(nameRegex1) == null) {
    return "Wrong";
  } else {
    return "Right";
  }
}

console.log(validName('abc'));
console.log(validName('abc-'));
console.log(validName('-abc'));
console.log(validName('-abc-'));
console.log(validName('a-b-c'));

console.log(validName1('abc'));
console.log(validName1('abc-'));
console.log(validName1('-abc'));
console.log(validName1('-abc-'));
console.log(validName1('a-b-c'));

FYI : You can use RegExp#test method for searching regex match and which returns boolean based on regex match.

if(nameRegex.test(n)){
  return "Right";
}
else{
  return "Wrong";
}


UPDATE : If you want only single optional - in between words, then use a 0 or more repetitive group which starts with -as in @WiktorStribiżew answer .
var nameRegex = /^[a-zA-Z]+(?:-[a-zA-Z]+)*$/;
like image 132
Pranav C Balan Avatar answered Sep 22 '22 10:09

Pranav C Balan