1.How to only allow alphabets for a field on a onchange client script? 2.How to allow only alphanumeric characters for a field on onchange client script?
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-24-2016 05:14 AM
2 REPLIES 2

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-24-2016 05:43 AM
Hello Mohammed,
You can use regular expression for that: like described here regex - Regular Expression for alphanumeric and underscores - Stack Overflow
For service-now I recommend using it in this manner:
var field = g.form.getValue('FIELD_YOU_WANT_TO_TEST');
var testREG = /^[a-zA-Z0-9_]*$/;
if (!testREG.test(field)) {
// HERE IS WHAT HAPPENS IF ENTERED VALUE DOES NOT MATCH
} else {
// HERE IS WHAT HAPPENS IF ENTERED VALUE DOES MATCH
}
Regards,
Michal

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-24-2016 05:47 AM
- You can customize the below code as per your requirement:
- function onLoad() {
- // CHANGE: 'short_description' to your field name
- g_form.getControl('short_description').observe('keypress',function(evt) {
- // Browsee black magic voodoo to get the CharCode pressed
- var charCode = (typeof evt.which == "number") ? evt.which : evt.keyCode;
- // If keyed pressed is the equivalent of 0-9 (charcode 48-57) or a period (charcode 46)
- if ((evt.keyCode >= 48 && evt.keyCode <= 57) || (evt.keyCode == 46)) {
- // Do nothing
- }
- else {
- // Suppress keypress
- evt.preventDefault();
- }
- });
- }