- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2017 12:03 PM
Hi Folks,
I am trying to validate an alpha-numeric field of length 10 in a catalog item. It's giving me an error even though I have used the alpha-numeric. I am doing this by onChange client script.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.hideFieldMsg('sap_customer_number');
var patt =new RegExp("^[a-zA-Z0-9]*$");
if (!patt.test(newValue)||patt.length !="10")
g_form.showFieldMsg('sap_customer_number','Invalid input','error');
}
Here are the results when I used this script:
Thanks in Advance,
SD
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2017 01:13 PM
That's strange, since it worked in my developer instance. Here's the full script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.hideFieldMsg('sap_customer_number');
var patt = new RegExp("^[a-zA-Z0-9]*$");
if (!patt.test(newValue + '') || newValue.length !="10")
g_form.showFieldMsg('sap_customer_number','Invalid input','error');
}
The only other thing I did was force newValue to a string in line 8.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2017 01:13 PM
That's strange, since it worked in my developer instance. Here's the full script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.hideFieldMsg('sap_customer_number');
var patt = new RegExp("^[a-zA-Z0-9]*$");
if (!patt.test(newValue + '') || newValue.length !="10")
g_form.showFieldMsg('sap_customer_number','Invalid input','error');
}
The only other thing I did was force newValue to a string in line 8.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2017 09:49 AM
Hi Fred,
Is there any possibility that we can get this on OnSubmit() Client script?
I wrote the following code but it is still submitting.
function onSubmit() {
//Type appropriate comment here, and begin script below
var alpha = new RegExp("^[a-zA-Z0-9]*$");
var projectKey=g_form.getValue('project_key');
alert("value"+projectKey);
var res=alpha.test(projectKey);
if (!res) {
g_form.showFieldMsg('project_key', 'Invalid input', 'error');
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2017 01:31 PM
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.hideFieldMsg('sap_customer_number');
if(/^[0-9a-zA-Z]+$/.test(newValue) && newValue.length==10){
g_form.hideFieldMsg('sap_customer_number');
}
else{
g_form.showFieldMsg('sap_customer_number','Invalid input','error');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2017 01:34 PM
Can you try
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}g_form.hideFieldMsg('sap_customer_number');
if (!/^[a-zA-Z0-9]{10}$/.test(newValue))
g_form.showFieldMsg('sap_customer_number','Invalid input','error');
}
Hope this helps.
Please feel free to connect, follow, mark helpful / answer, like, endorse.
John Chun, PhD PMP ![]() | ![]() |
Winner of November 2016 Members' Choice Award
2017 ServiceNow Community Leader (January)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2017 01:40 PM
You can simplify your test logic by checking for the length in RegEx using "{10}", which says "match exactly 10 times."