简体   繁体   中英

regular expresstion to match [/ , .] in ng-pattern

I am trying to create a expression so that input string must not contain forward slash, comma or a dot.

<form name="myForm">
    <div class="col-sm-4">
      <input class="form-control"
        type="text"
        data-ng-model="model_name"
        name="modelName"
        ng-pattern="/^[\/,.]/" required> 
      <div ng-messages="myForm.$submitted && myForm.modelName.$error" role="alert">
        <div class="alert alert-danger" ng-message="pattern">Special characters [/ , .] are not allowed.</div>
        <div class="alert alert-danger" ng-message="required">This field is required.</div>
      </div>
    </div>
</form>

The regular expression /^[\\/,.]/ properly matches all the characters inside the square brackets but the problem is that it is not allowing me to input normal strings such as abc or abc_def or any other strings. I don't have deep knowledge of regex and I am not getting any idea how to solve this problem. Please help.

Your regex is wrong, ^ must be inside [] in order to match all characters except /., put them in a [^/.,] , in regex, when using ^ inside a block [] will match anything but what is inside the [] .

在此处输入图片说明

When testing regex, this web it useful: http://www.regexpal.com/

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