Show alert onSubmit if field is not empty
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2016 09:52 AM
How can I set an onSubmit client script alert to display if a field is not empty? I'm not finding anything relevant other than things for catalog items which gives an error.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2016 10:19 AM
function onSubmit() {
//Type appropriate comment here, and begin script below
var your_field = g_form.getValue('u_field_name');
if (your_field == '') {
alert('u_field_name is blank');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2016 10:29 AM
Hi Paul,
You can navigate to Client Scripts, and create an onSubmit client script in there.
Thanks,
Berny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2016 10:31 AM
The following should be helpful:
http://wiki.servicenow.com/index.php?title=Client_Script_Best_Practices
http://wiki.servicenow.com/index.php?title=Client_Scripts#gsc.tab=0
Quote from the wiki:
3.2 onSubmit() Scripts
An onSubmit() script runs when a form is submitted. Typically, you use an onSubmit() script to validate things on the form and ensure that the submission makes sense. As such, onSubmit() scripts can potentially cancel a submission by returning false.
An onSubmit() script must contain a function named onSubmit().
For example, here is an onSubmit() script that prompts the user to confirm that a priority one ticket should really be submitted. If the user clicks Cancel in the confirmation dialog box, the submission is canceled.
function onSubmit() { var priority = g_form.getValue('priority'); if (priority && priority == 1) return confirm('Are you sure you want to submit a priority one ticket? The CIO will be notified!');} }The important thing to remember here is: to stop form submission, return false from your onSubmit() script.
Thanks,
Berny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2016 01:43 AM
HI,
Right click on the header -> configure -> client scripts -> new
From there choose onSubmit in the type field.
The code would be somewhat like this
function onSubmit(){
if(g_form.getValue("<field_name>")){
alert("Your text here");
}
}
Thanks