Click here to Skip to main content
14,977,979 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I need a regex expression to allow only certain text in a textbox
10 numbers: 0 to 926 letters: A to Z (upper cases)
 10 non-alphanumeric characters: - @ # $ % ( ) \ / . (dot)


The 5th Character should be an *

example
TLDZ*BB01S6N
22(O*AVB124C

What I have tried:

.keypress( function ( e ) {

var validChar = String.fromCharCode(e.which).search(/^[0-9A-Z]$/) === 0;

if (!validChar || ( (e.which != 46 || $(this).val().indexOf('*') != -5) &&
(e.which < 35 || e.which > 37) &&
(e.which < 40 || e.which > 42) &&
(e.which < 45 || e.which > 57) &&
(e.which < 64 || e.which > 90) &&
(e.which == 92) )) {
e.preventDefault();

}
Posted
Updated 1-Nov-17 22:50pm

Your regex allows only a single character.
Try this:
^[\w-@#\$*()\\/\.]+$
   
Comments
bmw318mt 2-Nov-17 5:26am
   
Thanks, how to force the 5th character to be an asteriks?
Note there should only be one asterik in the string
OriginalGriff 2-Nov-17 5:45am
   
Try:
^[\w-@#\$()\\/\.]{4}\*[\w-@#\$()\\/\.]+$


If you are going to use regexes, then get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900