- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2017 12:00 PM
I've been through a ton of Community articles and down so many rabbit holes trying to find an answer that works for this. When pulling up a Catalog Item, I have a form for users to request access to one of our datacenters. I can use lookups because the person that needs access could be a 3rd party vendor. So, the entire form needs the person to input everything. I have a working validation script for the email address and the phone #, but if the user inputs an invalid entry I would like the focus right back on that field.
Also, when the form loads I would like the focus to be on the first field. Does anyone have anything that actually works on a Catalog Item form?
I have tried this:
field='start_date';
var fieldToFocus = g_form.getControl(field);
fieldToFocus.focus();
return false;
and this:
window.setTimeout(cursor,0);
function cursor() {
var refocus = document.getElementById("IO:908699224ff76600932e8b8d0210c7ca");
refocus.focus();
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2017 12:13 PM
First of all, in my opinion, I think it's better to try avoiding usage of sys_id since there's a possibility for sys_id to change (just an opinion).
Btw, going for the answer, try with a jquery approach; just add this snippet to your client script:
(function($) {
var fieldname = 'start_date';
$(g_form.getControl(fieldname)).focus();
})(jQuery.noConflict());
1. First, we wrapped it in a scope-safe IIFE (Immediately-invoked function expression - Wikipedia ), it allows us to safely use jQuery without conflict in usage of "$".
Others use $j, but in my wide experience of using jQuery, this is the safest way of doing jQuery.
2. g_form.getControl(...) method returns the DOM of the specified field name. So we transform it into a jQuery DOM object, then we invoke jQuery.focus() method.
Let me know if it works. Cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2017 12:13 PM
First of all, in my opinion, I think it's better to try avoiding usage of sys_id since there's a possibility for sys_id to change (just an opinion).
Btw, going for the answer, try with a jquery approach; just add this snippet to your client script:
(function($) {
var fieldname = 'start_date';
$(g_form.getControl(fieldname)).focus();
})(jQuery.noConflict());
1. First, we wrapped it in a scope-safe IIFE (Immediately-invoked function expression - Wikipedia ), it allows us to safely use jQuery without conflict in usage of "$".
Others use $j, but in my wide experience of using jQuery, this is the safest way of doing jQuery.
2. g_form.getControl(...) method returns the DOM of the specified field name. So we transform it into a jQuery DOM object, then we invoke jQuery.focus() method.
Let me know if it works. Cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2017 12:37 PM
That didn't do anything different. Here's what I have:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (newValue === '') {
return;
}
//validates a valid email address for the onchange field
regex = /[A-Z0-9._%-]@[A-Z0-9.-]+\.[A-Z]{2,4}/ig;
var err_flag = 'false';
err_flag = regex.test(newValue);
if (!err_flag) {
alert('Invalid email address');
g_form.setValue('u_emailaddress', '');
(function($) {
var fieldname = 'u_emailaddress';
$(g_form.getControl(fieldname)).focus();
})(jQuery.noConflict());
}
}
Stanley Martin
ServiceNow Developer
Epiq
Corporate Services
Phone: 913-621-9824
Cell: 913-216-1031
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2017 07:07 AM
It seems I can get this to work on the initial form load to set focus on the first field:
function onLoad() {
var e = $('IO:89365d224ff76600932e8b8d0210c7b7'); // sys_id of the first input field
if (e)
e.focus();
}
What I'm trying to get to work is after validating an email address, if the format of the email address is invalid, I want to blank out the field and set the focus right back there. The above code isn't working for that piece.
Stanley Martin
ServiceNow Developer
Epiq
Corporate Services
Phone: 913-621-9824
Cell: 913-216-1031
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2017 07:43 AM
Allen,
Just an FYI that I found the issue. I've been using a tool called AlertNow which is a JavaScript Alert Replacement written by Andrew Lawlor. I found it on the ServiceNow Share. Great tool. However, it wasn't allowing me to set the focus back to the field. Going back to the default JavaScript alert function did work.
Thx for your assistance.
Stanley Martin
ServiceNow Developer
Epiq
Corporate Services
Phone: 913-621-9824
Cell: 913-216-1031