Answered by:
Dynamics CRM 2011 - UK phone number Javascript validation

Question
-
Hi,
I am trying to validate the mobilephone attribute through javascript using the contact entity in CRM 2011. Ideally this will need to fire onchange when the data includes any letters, equals 11 digits and must start with "07" (All UK mobile number start with 07). Can anyone assist with this requirement?
Thanks
SamMonday, September 16, 2013 8:28 AM
Answers
-
Hi,
Have you checked the "Pass execution context as first parameter" in the OnChange Event Screen???
Please go through this blog.
Nevertheless I'll walk you through the steps again,
1) Pass this function into your JavaScript file
function validateMobile(context) { var mobi = context.getEventSource().getValue(); mobiRegex = /[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/; //or mobiRegex = /07\d\d\d\d\d\d\d\d\d/; //must start with 07 (Lookup on the net for JavaScript phone regex to find your required Regex format) if (!mobi.match(mobiRegex)) { { context.getEventSource().setValue(''); event.returnValue = false; alert("The format of the mobile no is incorrect"); } } }
2) Double click on the telephone fields that you wish to add the validation event and select the JavaScript file and mention the function Name "validateMobile"
3) Check the Checkbox "Pass execution context as first parameter" and hit OK.
4) Make sure In ur OnLoad event your not firing any event that might be conflicting with our OnChange Event.
Please mark as answer and vote as helpful if it has solved your query or helped in solving your query
Regards
Darrel
- Marked as answer by samrr1875 Thursday, September 19, 2013 11:26 AM
Wednesday, September 18, 2013 3:47 AM
All replies
-
ValidateCharacters = function () { // get key code var key = event.keyCode; // only allow 0 through 9, parentheses (), dash -, period . and space if (!((key >= 48) && (key <= 57))) { //alert("Please enter a digit [0-9]" ); event.returnValue = false; return false; } } function validatePhone() { Xrm.Page.getControl('lbcre_phone1')._control._element.attachEvent("onkeypress", ValidateCharacters); } function validateLength(FieldName) { if (Xrm.Page.getAttribute(FieldName).getValue() != null) { if (Xrm.Page.getAttribute(FieldName).getValue().length != 11) { alert("Hi, you have entered " + Xrm.Page.getAttribute(FieldName).getValue().length + " digits whereas telephone number must be 11 digits long"); Xrm.Page.getControl(FieldName).setFocus(true); } } }
Regards Faisal
- Proposed as answer by nrodri Monday, September 16, 2013 10:05 AM
Monday, September 16, 2013 9:16 AM -
Only mobile number starts with 07
To check you can do something like this:-
var element = crmForm.all.new_telephonenumber.DataValue; var subSection = element.substring(2, 1); if (subSection == 7) { alert("It's a mobile no"); }
Regards Faisal
Monday, September 16, 2013 9:22 AM -
Hi,
JavaScript Function
testPhoneNumber = '07738273772'; // Typical UK mobile phone number
function validateMobile(context) {
var mobi = context.getEventSource().getValue();
mobiRegex = /[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/;//or mobiRegex = /07\d\d\d\d\d\d\d\d\d/; must start with 07 (Lookup on JavaScript phone regex to find your required //format)
if (!mobi.match(mobiRegex)) {
context.getEventSource().setValue('');
event.returnValue = false;
alert("The format of the mobile no is incorrect");
}
}
}//Tutorial on how to add the JavaScript
http://joegilldotcom.blogspot.com/2011/03/crm-2011-phone-format-validation-using.html
Hope this answers your query, Please Vote as helpful or Mark as answer if it have solved your query
Regards,
Darrel
Happy coding
- Edited by Darrel Dcosta Monday, September 16, 2013 9:30 AM
- Proposed as answer by nrodri Monday, September 16, 2013 10:05 AM
Monday, September 16, 2013 9:26 AM -
Hi
try this
function validatePhone(context) { var phone =Xrm.Page.Atribute("telephone1").getValue(); var sTmp = phone.replace(/[^0-9]/g, ""); phoneRegex = '/^\(?0\d{3,5}\)?\s?\d{3,4}\s?\d{3,4}(\s?\#(\d{4}|\d{3}))?$/'; if( !sTmp.match( phoneRegex ) ) { event.returnValue = false; alert("Phone must contain 10 numbers.") ; Xrm.Page.Atribute("telephone1".setValue(""); } else { var sTmpClean = sTmp; Xrm.Page.Atribute("telephone1".setValue(sTmp); } }
Monday, September 16, 2013 11:00 AM -
Hi Darrel,
Thanks for the response, I keep getting exceptions everytime I run this script onchange. I've setup the webresource with no problems however I cannot get this to execute. Do you have any ideas why?
Many Thanks
SamTuesday, September 17, 2013 11:44 AM -
Replace everywhere in the script
var phone =Xrm.Page.Atribute("telephone1").getValue();
with
var phone =Xrm.Page.getAttribute("telephone1").getValue();
Regards Faisal
- Edited by Faisal Fiaz Tuesday, September 17, 2013 12:24 PM
Tuesday, September 17, 2013 12:24 PM -
Hi,
Have you checked the "Pass execution context as first parameter" in the OnChange Event Screen???
Please go through this blog.
Nevertheless I'll walk you through the steps again,
1) Pass this function into your JavaScript file
function validateMobile(context) { var mobi = context.getEventSource().getValue(); mobiRegex = /[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/; //or mobiRegex = /07\d\d\d\d\d\d\d\d\d/; //must start with 07 (Lookup on the net for JavaScript phone regex to find your required Regex format) if (!mobi.match(mobiRegex)) { { context.getEventSource().setValue(''); event.returnValue = false; alert("The format of the mobile no is incorrect"); } } }
2) Double click on the telephone fields that you wish to add the validation event and select the JavaScript file and mention the function Name "validateMobile"
3) Check the Checkbox "Pass execution context as first parameter" and hit OK.
4) Make sure In ur OnLoad event your not firing any event that might be conflicting with our OnChange Event.
Please mark as answer and vote as helpful if it has solved your query or helped in solving your query
Regards
Darrel
- Marked as answer by samrr1875 Thursday, September 19, 2013 11:26 AM
Wednesday, September 18, 2013 3:47 AM