Answered by:
using regex with javascript

Question
-
Hi
I want a field called Account Code to only contain 3 digits and 3 numbers
If my understanding of regex is correct the patterns is "\w{3}\d{3}"
Im having a problem putting together the code as not to experienced with javascript
Can anyone help?
Thanks, Shaun
S.Harrison
Thursday, June 5, 2014 1:38 PM
Answers
-
which exact pattern you want? here some examples.
Assuming you get the accountcode value from the field with something like:
var accountnumber = Xrm.Page.getAttribute("accountnumber").getValue();
var pattern1 = /^[a-zA-Z]{3}\d{3}$/; // Abc123 3 lowercase o uppercase letters and 3 digits var pattern2 = /^[A-Z]{3}\d{3}$/; // ABC123 3 uppercase letters and 3 digits var pattern3 = /^\w{3}\d{3}$/; // A4c123 3 letters or digits and 3 digits if (pattern1.test(accountnumber)) { // the code is valid } else { // the code is not valid }
My blog: www.crmanswers.net - Rockstar 365 Profile
- Proposed as answer by Guido PreiteMVP Thursday, June 5, 2014 2:47 PM
- Marked as answer by Shaun Harrison Thursday, June 5, 2014 3:05 PM
Thursday, June 5, 2014 2:47 PM
All replies
-
which exact pattern you want? here some examples.
Assuming you get the accountcode value from the field with something like:
var accountnumber = Xrm.Page.getAttribute("accountnumber").getValue();
var pattern1 = /^[a-zA-Z]{3}\d{3}$/; // Abc123 3 lowercase o uppercase letters and 3 digits var pattern2 = /^[A-Z]{3}\d{3}$/; // ABC123 3 uppercase letters and 3 digits var pattern3 = /^\w{3}\d{3}$/; // A4c123 3 letters or digits and 3 digits if (pattern1.test(accountnumber)) { // the code is valid } else { // the code is not valid }
My blog: www.crmanswers.net - Rockstar 365 Profile
- Proposed as answer by Guido PreiteMVP Thursday, June 5, 2014 2:47 PM
- Marked as answer by Shaun Harrison Thursday, June 5, 2014 3:05 PM
Thursday, June 5, 2014 2:47 PM -
Hi
Thanks for the reply
I simply want the account code to have to contain 3 uppercase letters followed by 3 digits
From your example pattern2 would be the correct one?
Thanks, Shaun
S.Harrison
Thursday, June 5, 2014 2:50 PM -
Yes, pattern2 is the right one in your case.
My blog: www.crmanswers.net - Rockstar 365 Profile
Thursday, June 5, 2014 3:05 PM