Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to restrict specific characters in ext js textfield?

how to restrict specific characters(like apostrophe, !, ^ etc) in ext js textfield to enter with key strokes? I used below code which allows only mentioned characters

{
xtype:"textfield",
maskRe: new RegExp("[a-zA-Z1-9_\s@#$&~`]+$"),
allowBlank : false
}
like image 310
Arnav Joshi Avatar asked Dec 04 '25 16:12

Arnav Joshi


2 Answers

To specify restricted characters instead of allowed characters simply prepend a ^ inside the square brackets, which means "any character except...":

maskRe: /[^!\^]/

(= any character except ! and ^)

Also see this fiddle.

Also note that it's not necessary to use operators like +, ^ and $ because the regular expression used with maskRe is tested against each single character about to be entered, not against the value of the field.

like image 55
matt Avatar answered Dec 06 '25 08:12

matt


You need to add start of the line ^ anchor so that it would check for an exact string match and also you need to escape the backslash one more time.

new RegExp("^[a-zA-Z1-9_\\s@#$&~`]+$")
like image 24
Avinash Raj Avatar answered Dec 06 '25 08:12

Avinash Raj