How to check if the user enters email in correct format while submitting the service request form?

omkar2
Giga Contributor

The user should not be able to submit the form if the email is in incorrect format and prompted to correct it.

find_real_file.png

PS: I am using the username field above to auto-populate the user's email using catalog client script. Please help! Thanks.

1 ACCEPTED SOLUTION

Rajesh Mushke
Mega Sage
Mega Sage

Hey omkar,



why don't you create Email type variable



Email

The email variable creates a widget for users to enter and select an email address. It accepts only valid email expressions containing @ and domain information.



Example of an email variableExample of an email variable



Refer:


https://docs.servicenow.com/bundle/kingston-it-service-management/page/product/service-catalog-manag...




Thanks,
Rajashekhar Mushke
Rising star : 2022 - 2024
Community Leader -2018
Connect me on LinkedIn : Rajashekhar Mushke

View solution in original post

10 REPLIES 10

darlawolf
Giga Expert

I would recommend automatically populating the email address when a name is added on the "Contact Person" field.


If the value is bad or empty add an onChange script on "Please enter the E-mail address"and use an email regex pattern. If they give the wrong pattern, clear the value and show an error message.


omkar2
Giga Contributor

Thanks Darla. But you could you please help me write a script since I'm fairly new to scripting and SNOW development.


If you have Kingston, use the other suggestion. If not, then the script below should work with minor tweaks:


UI Type: All


Type: onChange


function onChange(control, oldValue, newValue, isLoading) {    



g_form.hideErrorBox('recipient_email');


if (isLoading || newValue == '') {


          return;


    }



//Validate the value in the email field is valid


var valid = validateEmail(newValue);


if (!valid)


g_form.showErrorBox('recipient_email', getMessage('Invalid email address'));


else


g_form.hideErrorBox('recipient_email');


}



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);


}


omkar2
Giga Contributor

Thank you so much, it worked but it still allows me to submit the request with bad email. Please let me know how to avoid that?