Regex Expression

Trupti Krishnam
Tera Contributor

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

4 REPLIES 4

hvrdhn88
Giga Patron

@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

Not applicable

Hi @Trupti Krishnam 

 

Please try this-

^(?!.* $)(?!.*,$)(?!.*, )(?!.*- )(?!.*-,$)[A-Za-z0-9,-]+$

Kindly mark my answer helpful and correct.

 

Regards,

Amit

Trupti Krishnam
Tera Contributor

it is allowing me whitespaces in the beginning of the line and end of the line

Not applicable

Hi @Trupti Krishnam 

 

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