To Check If a String Is Alphanumeric in JavaScript

Best way to alphanumeric check in JavaScript

You can use this regex /^[a-z0-9]+$/i

To check if a string is alphanumeric in javascript

Try this regex:

/((^[0-9]+[a-z]+)|(^[a-z]+[0-9]+))+[0-9a-z]+$/i

Which allows only Alphanumeric.

It doesn't allow:

  • Only Alpha
  • Only Numbers

Refer LIVE DEMO

Updated:

Below regex allows:

/^([0-9]|[a-z])+([0-9a-z]+)$/i
  • AlphaNumeric
  • Only Alpha
  • Only Numbers

javascript check if string contains only alphanumeric characters + other special characters

You can try something like this:

/^[a-z0-9-\'_\.,:\(\)&\[\]\/+=\?#@ \xC0-\xFF]+$/i

Logic

  • /a-z0-9/i will handle alphanumeric characters
  • \xC0-\xFF will handle foreign character
  • For symbols, you can list them individually.

An alternate could be listing all characters that are not allowed and use negate regex like

/^[^!~`\>\<]$/gi

Javascript regex to check for exact alphanumeric and special characters

You may use this regex with a character class that contains all allowed character sets:

/^[-\w .()&$#]+$/

RegEx Demo

  • You need to use anchors in your regex.

  • An unescaped hyphen should be at first or last position in a character class.


An alternative approach is to look for any not allowed character and fail the match if that is found:

function checkfirmName(str) {
const pattern = /[^-\w .()&$#]/; //not-acceptable char

if (pattern.test(str)) {
alert("Please only use \nUnderScore,Dot,(,Space,),&,$,# \nThese are only allowed\n");
return false; // bad user input
}

return true; // good user input
}

Regex to check if string contains Alphanumeric Characters and Spaces only - javascript

Your problem is that \w matches all alphanumeric values and underscore.

Rather than parsing the entire string, I'd just look for any unwanted characters. For example

var reg = /[^A-Za-z0-9 ]/;

If the result of reg.test is true, then the string fails validation.

RegEx for Javascript to allow only alphanumeric

/^[a-z0-9]+$/i

^ Start of string
[a-z0-9] a or b or c or ... z or 0 or 1 or ... 9
+ one or more times (change to * to allow empty string)
$ end of string
/i case-insensitive

Update (supporting universal characters)

if you need to this regexp supports universal character you can find list of unicode characters here.

for example: /^([a-zA-Z0-9\u0600-\u06FF\u0660-\u0669\u06F0-\u06F9 _.-]+)$/

this will support persian.

Javascript regex: check if a string is alphanumeric AND it contains a letter (at least)

I think your pattern is close, but I would use this:

/^(?=.*[0-9])[a-z0-9]{4,12}$/i

The only lookahead you need is one which asserts that there is a single number. There is no requirement for there to be any letters, so don't bother adding an assertion for that. Then, match any alphanumeric character 4 to 12 times.

console.log(/^(?=.*[0-9])[a-z0-9]{4,12}$/i.test('abc'));console.log(/^(?=.*[0-9])[a-z0-9]{4,12}$/i.test('123'));console.log(/^(?=.*[0-9])[a-z0-9]{4,12}$/i.test('abcd'));console.log(/^(?=.*[0-9])[a-z0-9]{4,12}$/i.test('Abc1'));

Regex to validate an alphanumeric string with optional space

(?=^.{6,7}$)^(([0-9] ?){4}( ?[a-zA-Z]){2})$

will match

  • 1111 ZZ
  • 111 1ZZ
  • 1 111ZZ
  • 1111ZZ
  • 1111 ZZ

but not

  • 9999 1A
  • 11111 Z
  • 1111111
  • 11 11 ZZ

https://regex101.com/r/lByOx6/1

EDIT: explanation

The "Positive Lookahead" part:

  • (?=^.{6,7}$) this only matches if the string meets the requirements, BUT it does not 'consume' the characters.
    • . is any character
    • {6,7} is about repetitions

so (?=^.{6,7}$) is matched if the string has 6 or 7 characters, no matter what

Then the following part already 'consumes' the string to say that I want at the start 4 repetitions of numbers and optionally space, and at the end 2 repetitions of letters and optionally space. The second part would accept strings such as 1 1 1 1 Z Z but as those are more than 7 characters, the first part wouldn't let the string match.

Validating alphabetic only string in javascript

Regular expression to require at least one letter, or paren, and only allow letters and paren:

function isAlphaOrParen(str) {
return /^[a-zA-Z()]+$/.test(str);
}

Modify the regexp as needed:

  • /^[a-zA-Z()]*$/ - also returns true for an empty string
  • /^[a-zA-Z()]$/ - only returns true for single characters.
  • /^[a-zA-Z() ]+$/ - also allows spaces


Related Topics



Leave a reply



Submit