locked
Prevent Users Saving the Form When Entering Invalid Email Address RRS feed

  • Question

  • Hello,

    I'm creating a javascript on the Account form with requirement as follow:

    a. When a text field is changed, check if it's using a valid email format. Also check that only one email address is allowed on this field.

    b. If a) is not met, then users are prevented from saving the form.

    I got a) portion working by adding an OnChange script on the field.

    However I try to use 'preventDefault' to prevent users from saving the form but not successful yet.

    I put below javascript on the Account OnSave event. However when I try to save the form with invalid email I got error message "UNABLE TO GET PROPERTY 'PREVENTDEFAULT' OF UNDEFINED OR NULL REFERENCE".

    Any suggestion is greatly appreciated.

    -tri

    function EmailTest(EmailField)
    {
        var Email = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+$/;
        if(Email.test(EmailField))
        {
            return true;
        }
        else
        {
            return false;
        }
    } 
    function CheckEmailString_OnSave()
    {
        try
        {
            var EmailString = Xrm.Page.data.entity.attributes.get("new_propertyemail").getValue();
    		if (EmailString != null)
    		{
    
    			var Flag = true;
    			 
    			//for(var i = 0;i < EmailArray.length;i++){
    		    
    		    if (!EmailTest(EmailString))
    				{
    					Flag = false;
    					//break;          
    				}
    			//}
    			if(Flag != true)
    			{
    				//prevents users from saving the form if not valid
    				Xrm.Page.context.getEventArgs.preventDefault();
    			}
    		}
        }
        catch(err)
        {
            alert(err.message);
        }
    }


    • Edited by triangular Wednesday, December 16, 2015 8:17 PM
    Wednesday, December 16, 2015 8:16 PM

All replies

  • Hi, try the following changes

    1. function CheckEmailString_OnSave(context)

    2. Check and update your onsave handler



    Eric UNG [Senior Analyst Programmer :: Sydney, Australia]

    Thursday, December 17, 2015 5:48 AM
  • I would suggest to use on change event instead on onsave event. You can prompt the use to correct email address on change because if the user have changed other fields then they will lose data.

    Regards Faisal

    Thursday, December 17, 2015 12:20 PM
  • Pass the context as first parameter and change the code as given below.

    function CheckEmailString_OnSave(excontext)
    {
        
    			if(Flag != true)
    			{
    				//prevents users from saving the form if not valid
    				excontext.getEventArgs().preventDefault();
    			}
    		
        }
        catch(err)
        {
            alert(err.message);
        }
    }


    • Edited by Kalim Khan Monday, December 21, 2015 1:18 PM
    Thursday, December 17, 2015 2:21 PM
  • Hi all, thanks for the suggested answers. Sorry I didn't realize there are many replies because for some reason I didn't get the email alerts...

    First of all I tried changing the OnSave event to pass the context and change the javascript function to receive the context parameter. Unfortunately this doesn't work and I still get the same error message...

    I also notice a slightly different syntax, instead of:

    context.getEventArgs.preventDefault();

    Somebody else posts:

    context.getEventArgs().preventDefault();

    This doesn't work either though saying 'getEventArgs' doesn't exist in the current context...

    I tried to wrap around the whole function in a try then catch function and replace the error message with something more meaningful. That doesn't work either.

    Unfortunately the biggest hit here is that even though saving an invalid field gives a pop-up error message about invalid function, it still lets the form save after the pop-up *sighs*

    Any suggestion is greatly appreciated. Thanks much,

    -tri

    Here's my code currently:

    function EmailTest(EmailField)
    {
        var Email = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+$/;
        if(Email.test(EmailField))
        {
            return true;
        }
        else
        {
            return false;
        }
    } 
    function CheckEmailString_OnSave(context)
    {
        try
        {
            var EmailString = Xrm.Page.data.entity.attributes.get("new_propertyemail").getValue();
    		if (EmailString != null)
    		{
                //removed for loop and separator as per Mark's request TFS Task 21050
    			//var separator = [" ", ",", ";", "|"];
    			//var EmailArray = EmailString.split(separator);
    			var Flag = true;
    			 
    			//for(var i = 0;i < EmailArray.length;i++){
    		    
    		    if (!EmailTest(EmailString))
    				{
    					Flag = false;
    					//break;          
    				}
    			//}
    			if(Flag != true)
    			{
    				//prevents users from saving the form if not valid
    				context.getEventArgs().preventDefault();
    			}
    		}
        }
        catch(err)
        {
            alert(err.message);
        }
    }


    • Edited by triangular Friday, December 18, 2015 10:44 PM
    Friday, December 18, 2015 10:43 PM
  • Sorry Edited post.. Did you try by debugging code? what version of CRm you are using?
    Monday, December 21, 2015 1:58 PM
  • Hi Kalim thanks for your reply. I'm using version 2013. 

    At this point I just try to out put a different error message instead of the regular one.... but the problem is the form gets saved anyway.

    Any suggestion is greatly appreciated. Thanks,

    -tri

    Monday, December 21, 2015 10:19 PM
  • Why not set form notification. Once you setup an error on form, it will show up the error message everytime user clicks save button.  However, you should clear it when you find that entered emailaddress is correct.
    Tuesday, December 22, 2015 7:52 PM
  • Hi Triangular,

    Your code is correct.Its working fine for me.

    Please make sure that the "on Save" event "Pass execution context as first Parameter" is checked(Check image posted by Eric ).

    Note: I observed that in chrome when i checked above parameter and published the entity.It was not taking changes.

    I suggest use IE 11 to save and publish your changes.

    Hope it helps.


    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer". Thanks and Regards, Mohammad Yusuf Ansari http://microxrm.blogspot.in

    Wednesday, December 23, 2015 1:37 PM
  • I would suggest to use Field Format as "Email" and you need not worry about email text validation. CRM will have it done for it.

    MaKeer | myencounterwithcrm.wordpress.com | CRM2011 User Settings Utility | CRM2011 Lookup Preview | CRM2011 Lookup Attribute Mapping | CRM2011 TreeView Control (Beta) | CRM2011 N:N Entity View (Beta) | CRM 2011 Global Quick Search (Beta)

    Sunday, December 27, 2015 6:56 PM