Answered by:
How do I format a field?

Question
-
I need to format a reference number as:
AA 12 34 56 A
How can I do this?
Sunday, June 23, 2013 2:26 PM
Answers
-
Guido, thanks but I need a format to be validated, for instance they can't type in a series of numbers. It has to be 2 letters, a series of numbers, then a letter at the end.
In this case you case use this javascript:
// get the value to validate var refNumber = Xrm.Page.getAttribute("new_reference").getValue(); // remove all spaces refNumber = refNumber.replace(/ /g,''); var validate = refNumber.match(/^[A-Z]{2}\d{6}[A-Z]$/); if (validate != null) { alert("valid"); } else { alert("not valid"); }
this will match exactly for string as AA123456A (2 uppercase letters, 6 digits, 1 uppercase letter)
if you want to check also for aa123456a you need to change the match to
/^[a-zA-Z]{2}\d{6}[a-zA-Z]$/
My blog: www.crmanswers.net
- Proposed as answer by Guido PreiteMVP Sunday, June 23, 2013 4:05 PM
- Edited by Guido PreiteMVP Sunday, June 23, 2013 4:48 PM fixed field name
- Marked as answer by Andrea Ricci Monday, June 24, 2013 4:31 PM
Sunday, June 23, 2013 4:05 PM
All replies
-
If you want to convert a field value from AA123456A to AA 12 34 56 A (adding a space every two characters) you can use this javascript:
var refNumber = Xrm.Page.getAttribute("new_reference").getValue(); refNumber = refNumber.replace(/(.{2})/g,"$1 "); alert(refNumber);
My blog: www.crmanswers.net
Sunday, June 23, 2013 3:29 PM -
Guido, thanks but I need a format to be validated, for instance they can't type in a series of numbers. It has to be 2 letters, a series of numbers, then a letter at the end.Sunday, June 23, 2013 3:39 PM
-
Guido, thanks but I need a format to be validated, for instance they can't type in a series of numbers. It has to be 2 letters, a series of numbers, then a letter at the end.
In this case you case use this javascript:
// get the value to validate var refNumber = Xrm.Page.getAttribute("new_reference").getValue(); // remove all spaces refNumber = refNumber.replace(/ /g,''); var validate = refNumber.match(/^[A-Z]{2}\d{6}[A-Z]$/); if (validate != null) { alert("valid"); } else { alert("not valid"); }
this will match exactly for string as AA123456A (2 uppercase letters, 6 digits, 1 uppercase letter)
if you want to check also for aa123456a you need to change the match to
/^[a-zA-Z]{2}\d{6}[a-zA-Z]$/
My blog: www.crmanswers.net
- Proposed as answer by Guido PreiteMVP Sunday, June 23, 2013 4:05 PM
- Edited by Guido PreiteMVP Sunday, June 23, 2013 4:48 PM fixed field name
- Marked as answer by Andrea Ricci Monday, June 24, 2013 4:31 PM
Sunday, June 23, 2013 4:05 PM -
Thanks. It was perfect.Monday, June 24, 2013 4:31 PM