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. Quote 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. Quote 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? Quote 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. Quote 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. Quote 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. Quote 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}$/ Quote 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.