Regex to trim white space and special characters from an input string before submission

chatsaurav19
Tera Contributor

Hi Team,

 

There is a requirement to trim white spaces and special characters from an input string before it gets submitted.

 

Ex. If user is inputting space in beginning and after the text ends and more than one space in between  then it should get trimmed.

<space><space><space><tab>TEST<space>TEST1<space><space><tab>TEST2<space><space><space>

should become

TEST<space>TEST1<space>TEST2 ( With only one space in between )

 

Moreover only these characters could be allowed : ( ) & -   (brackets, ampersand and hyphen) and no other characters

Ex. TEST - TEST1

       TEST & TEST1

       (TEST)

       

It will be great if someone could help me with the regex logic.

Regards,

Saurabh

 

 

 

 

 

10 REPLIES 10

Hi @John

 

Like Below:::

 

^(?!\d+$)(?:[a-zA-Z0-9][a-zA-Z0-9 &)(-]*)?$

 

Regards,

Saurabh