MrRalphMan Posted January 10, 2011 Share Posted January 10, 2011 Hi all, I want to use Regex to validate a string field. The field is 15 chars long and starts with three alpha chars. These can be either HPD or hpd. The remaining 12 chars can only be numeric. HPD000000000001 hpd000000000002 Many Thanks, Paul. Link to comment Share on other sites More sharing options...
Scoboblio Posted January 10, 2011 Share Posted January 10, 2011 My cat's breath smells like cat food. Sorry. Link to comment Share on other sites More sharing options...
JustGav Posted January 10, 2011 Share Posted January 10, 2011 Let me dig mine out, but I suspect starting with something as weird as \b\d{1,3}\d{1,12} (Although that is a number match). If it is a set length of 15 characters, are you not better off just doing a trim to 4-15 characters and parsing that? Link to comment Share on other sites More sharing options...
Attero Posted January 10, 2011 Share Posted January 10, 2011 /^[a-zA-Z0-9]{3}[0-9]{12}$/i The above validates that the first 3 are alphabetical letters and the rest are numerical only. Link to comment Share on other sites More sharing options...
Attero Posted January 10, 2011 Share Posted January 10, 2011 If you want to make sure the prefix is only ever hpd or HPD then: /^hpd[0-9]{12}$/i That will probably work. Link to comment Share on other sites More sharing options...
MrRalphMan Posted January 11, 2011 Author Share Posted January 11, 2011 Thanks dude. I got it working this morning using ^[hH][pP][dD][0-9]{12}$ Cheers, Paul. Link to comment Share on other sites More sharing options...
Attero Posted January 11, 2011 Share Posted January 11, 2011 Did you try my way? I mean, it's a bit shorter, and the "i" flag at the end indicates that the alphabetical characters are case-insensitive. Your way would work just as well anyway. If it could only be hpd or HPD (not hPd, HPd, HdP etc) then you could do: /^(hpd|HPD)[0-9]{12}$/ Link to comment Share on other sites More sharing options...
MrRalphMan Posted January 12, 2011 Author Share Posted January 12, 2011 I have to admit I didn't try your expression before knocking my one up, but using this online testing site the first one did not work. The second one does work as expected, so kudos with that one. Hopefully I don't need to play with RegEx much, a bit of a brain ache.... Cheers, Paul. Link to comment Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now