1

I am trying to restrict symbols (!@#$%^&*()_+, etc) in a text field. The application I am working on is using custom directives to generate text fields and I am not very familiar with it. I tried to implement the same by referring How to restrict special characters in AngularJS textbox [closed] but was not able to resolve my issue. I will attach all the related files below so that you will understand how framework is implemented.

I am trying to add the pattern restriction to a username form field. Please find the code below.

If we open the Element tab in Developer Tools, we will be able to see the directive name added to the input field as an attribute. I will copy the element tag from the developer window to show you the same:

<input type="text" id="txtName" name="name" ng-keyup="onkeyup({e : $event})" ng-blur="onblur({e : $event})" ng-model="nameModel" value="" placeholder="" class="form-control lvp-form-input ng-pristine ng-untouched ng-empty ng-invalid ng-invalid-required" required="" no-special-char="">

As you can see, no-special-char="" was added to the input field.

I am a fresher and still learning AngularJS. I am not familier with directives as well. Can anyone point out the mistake I am doing here and provide a solution? Also, if you think there's a better way to achieve this, please let me know the same. Thanks in advance.

1 Answer 1

1

Use ng-pattern like ng-pattern="/^[a-zA-Z0-9]*$/"

Sign up to request clarification or add additional context in comments.

2 Comments

But that will only create a validation error, correct? I want to restrict the symbols from getting entered in to the text field instead.
@Phoenix u should try creating custom directive with a function to check input characters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.