Regex to exclude numbers and special character except "-"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2022 04:43 AM
Hi All,
I have a client script to make first letter of every word as capital and articles like 'a', 'the'. etc in small letters. But in that I need to add more conditions.
1. numbers should not allowed
2. special character should not allowed except "-"
Client script OnChange of u_new_user
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var myText = newValue;
var myTextInTitleCase = toTitleCase(myText);
g_form.setValue('u_new_user, myTextInTitleCase);
function toTitleCase(e) {
var t = /^(a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|vs?\.?|via)$/i;
return e.replace(/([^\W_]+[^\s-]*) */g,
function(e, n, r, i) {
return r > 0 && r + n.length !== i.length && n.search(t) > -1 && i.charAt(r - 2) !== ":" && i.charAt(r - 1).search(/[^\s-]/) < 0 ? e.toLowerCase() : n.substr(1).search(/[A-Z]|\../) > -1 ? e : e.charAt(0).toUpperCase() + e.substr(1);
}
);
}
}
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2022 09:06 AM
https://regex101.com/ is really nice for testing regex patterns. Using that site, I came up with /[^a-zA-Z0-9-]/gm as the regex pattern. The site also provides an area to test the expression and an explanation of how it is evaluating.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2022 11:39 PM
Thanks @Claude DAmico this is awesome.
But how I can add this expression in my code?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2022 10:38 AM
How do you want to handle when those characters exist? You stated they should not be allowed, but there are options. Remove, replace, return a message to the user and clear the value, etc.?
For example: I want "a new 123 brown cat jumped over THE - lazy, brown dog." to display as "A New Brown Cat Jumped Over the - Lazy Brown Dog" so just replace values as needed.
var myText = "a new 123 brown cat jumped over THE - lazy, brown dog."; //this would actually be newValue in your case
var myTextInTitleCase = toTitleCase(myText);
gs.info(myTextInTitleCase);
//g_form.setValue('u_new_user, myTextInTitleCase);
function toTitleCase(e) {
var t = /^(a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|vs?\.?|via)$/i;
newText = e.replace(/([^\W_]+[^\s-]*)*/g, function(e, n, r, i) {/*gs.info("e is " + e); gs.info("n is " + n); gs.info("r is " + r); gs.info("i is " + i); */return r > 0 && r + n.length !== i.length && n.search(t) > -1 && i.charAt(r - 2) !== ":" && i.charAt(r - 1).search(/[^\s-]/) < 0 ? e.toLowerCase() : n.substr(1).search(/[A-Z]|\../) > -1 ? e : e.charAt(0).toUpperCase() + e.substr(1);});
newText2 = newText.replace(/([^a-zA-Z-]+[\s])/g," ");
return newText2;
}