Page 1 of 1

Email Validation...

Posted: 14 Aug 2017
by pselman
Is there a javascript function we can use to validate the format of the entry a user makes in a Email field? I know we can do this at SharePoint level but the use case we have requests that the validation is done when user has input the email address not when save is pressed..

Re: Email Validation...

Posted: 15 Aug 2017
by Nikita Kurguzov
Yes, it's totally possible to implement using JavaScript Editor inside of Forms Designer. For example, you can use code like this to check if email is valid or not:

Code: Select all

var $result = fd.field("FieldName").control()._el().find("br");

function validateEmail(email) {
  var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  return re.test(email);
}

function validate() {
  fd.field("FieldName").control()._el().find(".ms-formvalidation").remove();
  var email = fd.field("FieldName").value();
  if (validateEmail(email)) {
	return true;
  }
  $result.after('<span class="ms-formvalidation"> Enter a valid Email address.</span>');
  return false;
}

fd.onsubmit(function(){
  return validate();
});
Replace FieldName with the Internal name of your field first though.