Regarding client scripts

SNOW24
Tera Contributor

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. 

4 REPLIES 4

Samaksh Wani
Giga Sage
Giga Sage

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

Sohail Khilji
Kilo Patron
Kilo Patron

@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....

LinkedIn - Lets Connect

@Sohail Khilji 

 

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

Harish KM
Kilo Patron
Kilo Patron

hi you dont need a script for this, use oob regex module to validate number

1. go to regex module under service catalog

 

HarishKM_1-1690188526363.png

2. create a regex like below

HarishKM_2-1690188550196.png

3. go to your variable, under type specification choose your regex

HarishKM_3-1690188591310.png

 

 

NOTE: always avoid script when you can configure in servicenow

Regards
Harish