Required Regex for mobile number validation

AnandKumar1
Tera Expert

Hi Team,

i need regex for validating mobile number. Its simple that we need to validate 1 variable when user gives his mobile number it should start with country code (example : +91,+46). If not it should show an alert that your number is not valid as its missing with country code.

 

I have checked in previous community questions but for country code validation i am not able to find. Need assistance. 

 

Thanks.

8 REPLIES 8

As your script is running on change so it cannot stop from saving the record. You need to make your variable mandatory as @creativethinker suggested or if you just want to validate the number before submitting the request without making it mandatory then go for the onSubmit client script to revalidate the number before submission and return false in case of invalid input.

Also, instead of clearing the variable on an invalid number, you can instead display a field message[recommended] or alert to just inform the end-user about the invalid format. That would be a better UX.

Regards,
Muhammad

To prevent invalid values from being submitted, it's better to define a "Variable Validation Regex" rather than try to validate in a client script.

Steps:

  1. Open "Service Catalog" > "Catalog Variables" > "Variable Validation Regex" from Application Navigator
  2. Select "New"
  3. Enter regular expression for mobile phone number
    find_real_file.png
  4. Open Catalog Item with mobile phone number field
  5. Open Variable for mobile phone number
  6. In "Type Specification" table, select the regex for mobile phone number defined above
    find_real_file.png

Now, a field message will appear if the mobile number is in incorrect format.

find_real_file.png

Selecting "Order Now" will also show an error in the form and prevent the form from being submitted.

find_real_file.png

 

I have done the same steps using different regex rules, but for some reason the validation message from the Question Regular Expression only shows if I use the 'Try Now' in the catalog Item form. 

find_real_file.png

If I try to run it through service portal, I see the space open below the field where the message should be and if I inspect that area using the browser, I see that my text is there, but it is formatted as white text on a transparent background...

find_real_file.png

Any ideas?

Ct111
Giga Sage
Hi Anand, This is last example I can give you for achieving your objective plz check once for onChange .Take this as reference EXAMPLE 4: VERIFY A PHONE NUMBER Link : ----------+ https://www.servicenowelite.com/blog/2014/2/19/client-scripts-examples