Email Validation...

Discussions about Forms Designer for SharePoint 2013 / 2016 and Office 365.
Locked
pselman
Posts: 24
Joined: Wed Nov 04, 2015

14 Aug 2017

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..

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

15 Aug 2017

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.
Cheers

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 6 guests