variable length restriction and validation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2014 12:54 PM
Hi All,
I have a requirement where one variable should accept only numbers and should be limited to 10 characters.
I have wrote a below onchange script to accept only numbers and onload script for field length.
Unfortunately onload script is not working.. any help please........
OnChange
function onChange(control, oldValue, newValue, isLoading) {
var callBack = g_form.getValue('callback');
if(callBack.match(/\D/)){
alert('Please enter only numbers!');
g_form.setValue('callback,'');
}
}
Onload
function onLoad() {
var call_back = g_form.getControl('callback');
alert(call_back);
call_back.maxLength = 10;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2014 01:20 PM
What table is this running on? We have no fields in our database named "callback". Therefore I suspect this is a custom field, and that it's real name is probably "u_callback". Unless this is a service catalog variable?
If this is a database field, then you can set the maximum length by personalizing the dictionary.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2014 01:28 PM
To limit input length, add max_length=10 to the variable attributes.
Variable Types - ServiceNow Enterprise Wiki
Blog: https://sys.properties | Telegram: https://t.me/sys_properties | LinkedIn: https://www.linkedin.com/in/slava-savitsky/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2014 02:27 PM
onChange vs onLoad:
Your onChange is running simultaneously as an onLoad AND as an onChange.
You need to put an 'isLoading' check in to prevent it running on page load:
function onChange(control, oldValue, newValue, isLoading) {
if(!isLoading) {
var callBack = g_form.getValue('callback');
if(callBack.match(/\D/)){
alert('Please enter only numbers!');
g_form.setValue('callback,'');
}
}
}
Regular Expression:
Your regular expression "/\D/" will only match NON digit characters. To match a digit use /\d/, to match only 10 digits, use /\d{10}/. I'd personally also start matching at the start of the string, resulting in: /^\d{10}/.
You are using .match() when a .test() seems to fit better for this use case. I recommend using .test() as this returns either true or false. .match() returns an array of results if a match is successful. Either should work for this case but .test() is tremendously faster (more so for larger record sets).
Try:
function onChange(control, oldValue, newValue, isLoading) {
if(!isLoading) {
var callBack = g_form.getValue('callback');
if(callBack.test(/^\d{10}/)){
alert('Please enter only numbers!');
g_form.setValue('callback,'');
}
}
}
Form Variable:
If you have to restrict the variable length to 10 characters/digits, set it from the variable's variable attributes (max_length=10) and not from an onLoad script. If it were me I'd completely remove the onLoad.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2015 02:34 AM
Hi
Its giving me an error while using above field
JavaScript parse error at line (6) column (40) problem = unterminated string literal
- Kailas