Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript regex for alphanumeric string with length of 3-5 chars

I need regex for validating alphanumeric String with length of 3-5 chars. I tried following regex found from the web, but it didn't even catch alphanumerics correctly.

var myRegxp = /^([a-zA-Z0-9_-]+)$/; if(myRegxp.test(value) == false) {     return false; } 
like image 582
newbie Avatar asked Jan 20 '11 08:01

newbie


People also ask

How do I find the length of a string in regex?

To check the length of a string, a simple approach is to test against a regular expression that starts at the very beginning with a ^ and includes every character until the end by finishing with a $.

How do I allow special characters in regex?

You can use this regex /^[ A-Za-z0-9_@./#&+-]*$/. To know more about regex ,watch this videos. You can use this regex /^[ A-Za-z0-9_@./#&+-]*$/.

How do I restrict only alphanumeric in JavaScript?

You will use the given regular expression to validate user input to allow only alphanumeric characters. Alphanumeric characters are all the alphabets and numbers, i.e., letters A–Z, a–z, and digits 0–9.


1 Answers

add {3,5} to your expression which means length between 3 to 5

/^([a-zA-Z0-9_-]){3,5}$/ 
like image 111
Sachin Shanbhag Avatar answered Sep 19 '22 03:09

Sachin Shanbhag