0

I'd want to validate a username using a regular expression, however the expression I used worked well on https://regexr.com/  but not in my code. I just want user can add only Alphanumeric and a space between them

var letters = /^[A-Za-z]+$/g;

function validate() {
  var input = document.getElementById("fname").value;
  if (input.match(letters)) {
    document.getElementById("name").innerHTML = "** Only alphabets are allowed in name.";
    return false;
  }
}
<input id="fname">
<input type="button" onclick="validate()" value="Check">
<br>
<span id="name"></span>
depperm
  • 10,606
  • 4
  • 43
  • 67
  • 4
    It is usually a bad idea to limit user names, for example Peter O'Donelly-Smith might not be happy with that. *Why* do you need to limit user names? That might help us find a better solution for you. – Andrew Morton Oct 08 '21 at 17:45
  • I actually want to validate user names so that a user can input their names in the format "Ali Ahmed." – Bisma Amjad Oct 08 '21 at 17:49
  • 1
    @AndrewMorton I don't agree... messy username will be a bad idea. Will look bad for other users, and will be annoying, if someone will exploit it. Every decent service does limit user names. – Flash Thunder Oct 08 '21 at 17:51
  • Does this answer your question? [Regular expression "^\[a-zA-Z\]" or "\[^a-zA-Z\]"](https://stackoverflow.com/questions/2790813/regular-expression-a-za-z-or-a-za-z) – Andrew Morton Oct 08 '21 at 17:51
  • 1
    @FlashThunder Try telling that to anyone who wants their name to be represented correctly, but the system does not allow it. – Andrew Morton Oct 08 '21 at 17:56
  • @BismaAmjad There is a full stop, `.`, at the end of your example `Ali Ahmed.`. (There is a difference between using speech marks for speech, where following punctuation appears inside the quote marks, and quoting exact text for programming purposes.) – Andrew Morton Oct 08 '21 at 17:58
  • @AndrewMorton without any validation, you could use something like `????don't really care and it doesn't need to be evaluated as html to be annoying` or even worse ... would you really like to have something like that in your db? I guarantee that there is no service that doesn't restrict the name string in this word, that has more than 100000 users. If there is, show me I'm wrong. Name it. – Flash Thunder Oct 08 '21 at 17:58
  • @FlashThunder It should not matter what is in the database, it will be HTML-encoded for display. *Won't it? ;)* – Andrew Morton Oct 08 '21 at 17:59
  • @AndrewMorton as I said in the name, it can't be evaluated, still a trash for other users, that don't want to see it. And there are a lot of people at least trying to exploit it. Even in SO you can't have such a name. – Flash Thunder Oct 08 '21 at 18:00
  • @BismaAmjad If you are looking at Persian names, then Mas'ud would be unlucky. – Andrew Morton Oct 08 '21 at 18:03
  • @FlashThunder There is an abundance of abhorrent ideas representable with just the letters a-z, which suggests that your point is not sufficiently well thought out if you are proposing that only allowing the characters in the range [A-Za-z] will inhibit them. – Andrew Morton Oct 08 '21 at 18:37
  • @AndrewMorton I agree that i should be extended, but not removed. – Flash Thunder Oct 08 '21 at 18:59

1 Answers1

1

Your current function is "finding alphanumeric characters". So, when you run validate(), it is returning either a valid Array, or null. To get spaces use /^[\w\-\s]+$/ although this will return true for multiple spaces. I'm not sure your use case.

Unless you want to change your regex, you need to show your warning when input.match() returns a falsey value. To do this, Place ! before your input.match() function

var letters = /^[\w\-\s]+$/;

function validate() {
  document.getElementById("name").innerHTML = "";

  var input = document.getElementById("fname").value;

  if (!input.match(letters)) {
document.getElementById("name").innerHTML = "** Only alphabets are allowed in name.";
return false;
  }
}
<input id="fname">
<input type="button" onclick="validate()" value="Check">
<br>
<span id="name"></span>
Brian Scramlin
  • 306
  • 2
  • 11
  • Yeah, guys I should have read the question more specifically instead of just bug-fixing the existing code. I will update or remove my answer. – Brian Scramlin Oct 08 '21 at 17:58
  • Also, is this duplicate of https://stackoverflow.com/questions/13283470/regex-for-allowing-alphanumeric-and-space ? – Brian Scramlin Oct 08 '21 at 18:04