Regex Expression
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2024 08:51 AM
Hi I have a requirement where the string entered in the multiline text Only allow for letters, numbers, hyphens (-) and commas (,). Do not allow for spaces at the beginning or at the end and even between the character .., I m not able to write the expression for the spaces.. Can anyone help
Thank you

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2024 09:50 AM
@Trupti Krishnam Try with below regex
^[a-zA-Z0-9,-]+$
Eg: Client side scripting
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var re = /^[a-zA-Z0-9,-]+$/ ;
alert(re.test(newValue)); // Add further script based on your need
}
Thanks,
Harsh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2024 10:41 AM
Please try this-
^(?!.* $)(?!.*,$)(?!.*, )(?!.*- )(?!.*-,$)[A-Za-z0-9,-]+$
Kindly mark my answer helpful and correct.
Regards,
Amit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2024 11:07 PM
it is allowing me whitespaces in the beginning of the line and end of the line
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2024 11:16 PM
let input = " your,string,here ";
let trimmedInput = input.trim(); // Remove leading and trailing spaces
let regex = /^(?!.* $)(?!.*,$)(?!.*, )(?!.*- )(?!.*-,$)[A-Za-z0-9,-]+$/;
let isValid = regex.test(trimmedInput);
console.log(isValid); // This will be `true` or `false` based on your input
Can you try above approach-
Regards,
Amit