Checking text field details RRS feed

  • Question

  • CRM2016

    I have a text field (6) which I need to check;

    1. only contains 0123456789

    2. is 6 in length

    3. display a warning and stops the record being saved if either the two conditions above are incorrect

    I believe this can be done in JScript if anyone can help

    Dont ask me .. i dont know

    Tuesday, May 10, 2016 3:05 PM


  • You can add the below JavaScript to the on save event of your form. Make sure that you tick the 'Pass execution context as first parameter' box when you add it and change "your_field_name" to your field name

    function ValidateSomeField(context)
    	var str = Xrm.Page.getAttribute("your_field_name").getValue(); //change this to your field name
    	var reg = /^[0-9]{6}$/; //Regular expression for 6 digit number
    	if(!reg.test(str)) //If not in format specified above, stop the save and show a message
    		alert("Field name should contain 6 numbers only"); //Show a message
    		var saveEvent = context.getEventArgs();
    		saveEvent.preventDefault();  //stop the save

    Neil - My CRM Blog

    • Edited by Neil McD Tuesday, May 10, 2016 4:06 PM
    • Marked as answer by Pete Newman Tuesday, May 10, 2016 6:42 PM
    Tuesday, May 10, 2016 4:01 PM