Regarding client scripts
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2023 12:13 AM
If user enters other than numeric values and less than or more 10 digits stop form submitting and need to get pop up message. please provide code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2023 12:18 AM - edited 07-24-2023 12:19 AM
Hello @SNOW24
You need to write onLoad() Client Script
function onLoad() {
var pattern = /^[(]?(\d{3})[)]?[-|\s]?(\d{3})[-|\s]?(\d{4})$/;
var phone = g_form.getValue('mobile_field');
if(!pattern.test(phone)){
alert('Phone enter a valid phone number');
g_form.setValue('mobile_field', '');
return false;
}
}
Plz Mark my Solution as Accept and Give me thumbs up, if you find it Helpful.
Regards,
Samaksh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2023 01:15 AM
@SNOW24 ,
Please use OnChange Client script for this :
var phone = g_form.getValue('field_value'); // enter the field value in place for field_value
if(phone.length!== 10 && !isNaN(phone)){
alert("number should be 10 digits");
g_form.setValue('field_value', '');
}
Adjust the fields and code on the above script accordindly...
@Samaksh Wani , Why Onload client script when the use is about the enter the value in field ? Onload works only when the page is being loaded.
☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2023 01:42 AM - edited 07-24-2023 01:43 AM
I know very much better, onLoad() works only on page load, As it is cleary mentioned in the Question, that requirement should hit on page load, thats why gave the solution of onLoad().
Also I can see in your script you are using only length, to check. But there are many use cases to validate phone number. so regular expression is the right way for validation.
Regards,
Samaksh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2023 01:50 AM
hi you dont need a script for this, use oob regex module to validate number
1. go to regex module under service catalog
2. create a regex like below
3. go to your variable, under type specification choose your regex
NOTE: always avoid script when you can configure in servicenow
Harish