- 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 07:39 AM
Can you try this and share alerts?:
function onLoad() {
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_by', parseData.mobile_phone);
var phone = parseData.mobile_phone;
var pattern = /^[+1]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*$/;
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');
}
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-08-2020 01:07 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-08-2020 11:12 PM
If that is the only alert, then the phone number is not cleared by your script, right?
So it will still set the field to (999) 999-9999.
Or is that field also running the onChange script, which will not accept (999) 999-9999?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-08-2020 11:18 PM
There are two fields requested for and requested by:
Requested by is read only and will only populate value.
But am getting the value in alert and that's also after removing the format.
And the field is not setting to any value and is cleared.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-08-2020 11:23 PM
But the only clearing of value happens where you should get additional alerts, which you do not get.
And without all the validation you are able to set the value? So like this:
function onLoad() {
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_by', parseData.mobile_phone.toString());
}
}
Also test without the toString to see if the original code works.