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

can you paste your script please?

omkar2
Giga Contributor

Thanks for getting back! I just replaced my email field value with yours from the script.

 

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

g_form.hideErrorBox('door_schedules_email');
if (isLoading || newValue == '') {
return;
}

//Validate the value in the email field is valid
var valid = validateEmail(newValue);
if (!valid)
g_form.showErrorBox('door_schedules_email', getMessage('Invalid email address'));
else
g_form.hideErrorBox('door_schedules_email');
}

function validateEmail(door_schedules_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(door_schedules_email);
}

omkar2
Giga Contributor

Hi Darla,

I'm still unable to figure out how to avoid the submit request even if the email is invalid.

Please help.

Here's the script:

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

g_form.hideErrorBox('door_schedules_email');
if (isLoading || newValue == '') {
return;
}

//Validate the value in the email field is valid
var valid = validateEmail(newValue);
if (!valid)
g_form.showErrorBox('door_schedules_email', getMessage('Invalid email address'));
else
g_form.hideErrorBox('door_schedules_email');
}

function validateEmail(door_schedules_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(door_schedules_email);
}

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

Hi Rajesh. Thanks for the solution. I got it working on my servicenow instance. However, the changes are not getting reflected at the user service portal. It still shows the text field instead of the email field.



find_real_file.png         .        


Servicenow




Service Portal                                    



Please help!