Restrict input with regex

Jan 2 2020 11:27 PM
HI,
 
I have a input requirement of string pattern from front end.
 
the string can be aa-bbb-cccc the first word would be minimum 2 then second word would be between (3 to 5 character) and the last word can be (4 to 7 character)
 
each word is separated by a hyphen.
 
so that i have written one regular expression but it is not working can any one help me.
 
i am not able to find the problem.
  1. var CustomeExp=/^([a-zA-z]{2})-([a-zA-Z]{3,5})-([a-zA-Z]{4,7})$/;  
  2. here is my code which...  
  3. <!DOCTYPE html>  
  4. <html>  
  5. <head>  
  6. <script data-require="jquery@*" data-semver="3.2.1" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>  
  7. </head>  
  8. <body>  
  9. Enter your text :  
  10. <br>  
  11. <input type="text" onkeypress="return validateKeypress(CustomeExp);">  
  12. </body>  
  13. </html>  
JS
  1. var CustomeExp=/^([a-zA-z]{2})-([a-zA-Z]{3,5})-([a-zA-Z]{4,7})$/;  
  2.   
  3. function validateKeypress(validChars) {  
  4. //alert(validChars);  
  5. var keyChar = String.fromCharCode(event.which || event.keyCode);  
  6. //alert(keyChar);  
  7. return validChars.test(keyChar) ? keyChar : false;  
  8. }  
Thanks

Answers (2)