I have a form in that i want to do Email Id validation it should be abc123@gmail.com

Veera raju
Tera Expert

Hi,

I have a form in that i want to do Email Id validation it should be abc123@gmail.com

please help me on the same functionality.

find_real_file.png

 

Regards,

 

Alikutty A
Tera Sage

Hi,

You can write an on change client script on Email ID field with the following script in it.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}else if(oldValue != newValue){
		var reg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
		if (!reg.test(newValue)){
			alert('You should enter a valid email address');
			g_form.clearValue('u_email_id'); //Replace email id field name
		}
	}
}

Thanks!

Priti Golam1
Tera Expert

Hi Veera,

For this u can first create a script include and call this script include on server side using business rule. I have tried this practically and it works.

1) Create script include

Name: validateEmailAddress
        API Name: (this field value is automatically populated)
        Application: (this field value is automatically populated)
        Accessible from: All application scopes
        Active: Selected (checked)
        Description: On demand Script Include to validate email address syntax using regular expressions.
 
function validateEmailAddress(emailStr){
// Use JavaScript coercion to guarantee emailStr is a string
emailStr = emailStr + '';
// Compare emailStr against the allowed syntax as specified in the regular expression
// If emailStr has allowed syntax, return true, else return false
if(emailStr.match(/^(([^<>()\[\]\\.,;:\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 true;
}
else {
return false;
}
}
 
 
2) Create business rule
  1. Name: Email Address Syntax Validate
            Table: NeedIt [x_<your_company_code>_needit_needit]
            Active: Selected (checked)
            Advanced: Selected (checked)
  2. Switch to the When to run section and continue configuring the Business Rule:

            When: Before
            Insert: Selected (checked)
            Update: Selected (checked)
  3. Click the Submit button.

  4. Switch to the Advanced section.

 

var isEmail = validateEmailAddress(current.u_requested_for_email);
// If isEmail is false (email address syntax is not valid) do not save
// the record. Write an error message to the screen.
if(isEmail == false){
gs.addErrorMessage(current.u_requested_for_email + " is not a valid email address. You must provide a valid email address.");
current.setAbortAction(true);
}

And also refer below link,

https://developer.servicenow.com/app.do#!/lp/servicenow_application_developer/app_store_learnv2_scri...

If u find my answer helpful. Kindly mark it as correct or helpful.

Thanks,

Priti

Rahul Kumar17
Tera Guru

hi

check this code in background script

var emailAddr='abc123@gmail.com';
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailAddr))
  {
    gs.print('yes')
  }
else{
   gs.print("You have entered an invalid email address!")
}
If my response helped please mark it correct and close the thread.

Thanks,
Rahul Kumar

View solution in original post

asifnoor
Kilo Patron

Hi Veera,

Do you want to validate the email format like 3 chars and 3 digits followed by @gmail.com? or do you want to validate whether the entered email is abc123@gmail.com or not? What is your requirement?