简体   繁体   中英

facing issue while restrict number and special characters in angularjs

I want to restrict the special characters and space in the input text field. I used the

ng-pattern="/^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/"

to restrict the special characters. but its not working. How can i achieve that?

here is my html code:

<form name="JobDescriptionForm" id="JobDescriptionForm" class="form-horizontal" role="form" novalidate>
    <div class="col-md-12 col-sm-12 col-lg-12 nopadding select-job">
        <label for="createJob" class="col-md-12 col-sm-12 col-lg-12 nopadding"> Create Job </label>
        <input type="text" ng-model="price" name="price_field" ng-pattern="/^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/" required>
        <span ng-show="JobDescriptionForm.price_field.$error.pattern">Not a valid number!</span>
    </div>
</form>

I' m referring this code because I'm little confused for is there any need to write something in Controller? What to do in this case?

Why don't you use ng-pattern="/^[a-zA-Z0-9]+$/" if all that you want is to restrict special characters and space?

And check your code as well, your end tag for "form" has a typo, you wrote "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM