how to check single line text should contain data in format "firstname lastname"

saranyavs
Tera Expert

Hi team,

 

In catalog item there is a field called 'name' as single line text, this should allow data only in below format. how to write the script ?

Firstname<space>Lastname

5 REPLIES 5

vaishali231
Tera Guru

hey @saranyavs 

Create a Catalog Client Script
Type: onSubmit
UI Type: All

Script:

function onSubmit() {

var name = g_form.getValue('name'); // variable name

// Regex
var regex = /^[A-Za-z]+ [A-Za-z]+$/;

if (!regex.test(name)) {
g_form.showFieldMsg('name', 'Please enter value in format: Firstname Lastname', 'error');
return false;
}

return true;
}

 

*************************************************************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.

Doing so helps others in the community and encourages me to keep contributing.

 

Regards

Vaishali Singh

 

 

Ankur Bawiskar
Tera Patron

@saranyavs 

why are you expecting user to remember any person's first name and last name while entering?

why not use reference variable?

What's your actual business requirement? what's that form used for? what that variable used for?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

This is for new account creation, hence reference field cannot be used.

and the variabe is used for orchestration for AD account creation.

Tejas Adhalrao
Tera Guru

Hi @saranyavs  ,

 

you can write the on change client script for your name  field.

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

    if (isLoading || newValue == '')
        return;

    var regex = /^[A-Za-z]+ [A-Za-z]+$/;

    if (!regex.test(newValue)) {
        g_form.showFieldMsg('u_name',
            'Format must be: Firstname Lastname (Example: John Doe)',
            'error'
        );
    } else {
        g_form.hideFieldMsg('u_name');
    }
}

 

 

 

 If you found my solution helpful, please mark it as Helpful or Accepted Solution...!

thanks,

tejas

Email: adhalraotejas1018@gmail.com

LinkedIn: https://www.linkedin.com/in/tejas1018