- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-08-2020 03:24 AM
There are two phone number field for requester for and requested by:
Phone Number validation for populating phone number field on load(it takes value of phone number from user table)
It should only accept these formats-(999) 999-9999, 999-999-9999, and 9999999999
I have this code for on change: working fine populating numbers for these formats-(999) 999-9999, 999-999-9999, and 9999999999 (REQUESTED FOR)
var pattern = /^[+1]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*$/;
var phone = g_form.getValue('phone_number_requested_for');
if(!pattern.test(newValue)){
alert('Phone enter a valid phone number');
g_form.setValue('phone_number_requested_for', '');
}
else{
phone = phone.replace(/\D/g,'');
var regex = /^\d{10}$/;
var is_valid = regex.test(phone);
if(!is_valid){
alert('Invalid phone number. Please enter 10 digits/ Do not add country code');
g_form.clearValue('phone_number_requested_for');
}
}
}
But same code is not working for onLoad:(REQUESTED BY)
function onLoad() {
//Type appropriate comment here, and begin script below
var user = g_form.getValue('requested_by');
var ga = new GlideAjax('InternalApplicationUtil');
ga.addParam('sysparm_name', 'getValues');
ga.addParam('sysparm_val', user);
ga.getXML(getdata);
function getdata(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var parseData = JSON.parse(answer);
g_form.setValue('email_requested_by', parseData.email);
g_form.setValue('email_requested_for', parseData.email);
g_form.setValue('phone_number_requested_for', parseData.mobile_phone);
// g_form.setValue('phone_number_requested_by', parseData.mobile_phone);
var phone=parseData.mobile_phone;
//alert(check);
var pattern = /^[+1]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*$/;
//var phone = g_form.getValue('phone_number_requested_for');
if(!pattern.test(phone)){
alert(phone+" f");
alert('Phone enter a valid phone number');
g_form.setValue('phone_number_requested_by', '');
}
else{
phone = phone.replace(/\D/g,'');
var regex = /^\d{10}$/;
alert(' m '+phone);
var is_valid = regex.test(phone);
if(!is_valid){
alert('d '+phone);
alert('Invalid phone number. Please enter 10 digits/ Do not add country code');
g_form.clearValue('phone_number_requested_by');
}
}
}
}
Can someone tell what am I missing here?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-08-2020 10:43 PM
Hi Deepali,
Firstly, can you try below modified client script for onChange:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var oldV = oldValue;
var validRegExp = /^\(?(\d{3})\)?[ -]?(\d{3})[ -]?(\d{4})$/;
var checkPhone = g_form.getValue('phone_number_requested_for');
if (checkPhone.search(validRegExp) == -1)
{
alert('Enter valid mobile phone in : (999) 999-9999, 999-999-9999, and 9999999999 formats');
g_form.setValue('phone_number_requested_for',oldV);
}
}
Secondly,
try implementing same regex expression check for On Load aswell.
ex:
//alert(check);
var validRegExp = /^\(?(\d{3})\)?[ -]?(\d{3})[ -]?(\d{4})$/;
//var phone = g_form.getValue('phone_number_requested_for');
if (phone.search(validRegExp) == -1)
{
alert('Enter valid mobile phone in : (999) 999-9999, 999-999-9999, and 9999999999 formats');
g_form.setValue('phone_number_requested_by','');
}
else{
g_form.setValue('phone_number_requested_by', phone);
}
}
Note:
since we are using else condition to set phone number if its valid, you can remove below line:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-08-2020 03:47 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-08-2020 03:49 AM
Yes it do have value that is coming from user table(user's phone number)
I have edited the question for more clarity please check it once.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-08-2020 04:02 AM
Few questions:
- Can you share the script include as well?
- Do you get a value back from the script include?
- Can you share the alert results?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-08-2020 04:20 AM
1.Script Include:
getValues: function() {
var val = this.getParameter('sysparm_val');
var jsonObj = {};
var gr = new GlideRecord('sys_user');
if (gr.get(val)) {
jsonObj.email = gr.getDisplayValue('email');
jsonObj.mobile_phone = gr.getDisplayValue('mobile_phone');
var json = new JSON();
var data = json.encode(jsonObj);
}
return data;
},
2. Yes am getting correct value from script include
3. Alert
: