简体   繁体   English

仅在字段为$ dirty时如何应用ng-pattern

[英]How to apply ng-pattern only when field is $dirty

I have an input box that has an ng-pattern on it for basic link validation. 我有一个输入框,上面有一个ng模式,用于基本链接验证。 Our PM wants us to have the 'http://' text already in the input before the user starts editing it...however i can't just put this text there as it breaks the ng-pattern validation and thus, doesn't show up. 我们的PM希望我们在用户开始编辑之前在输入中已经有'http://'文本...但是,我不能只是将其放在此处,因为它破坏了ng-pattern的验证,因此,没有。露面。

<input type="text" ng-model="$parent.emailData.setting.button[2].link" 
 name="button2link" 
 ng-class="{invalid: step3Form.button2link.$invalid}"
 ng-pattern="link_pattern"/>

For reference, link_pattern is defined as 供参考,link_pattern定义为

 $rootScope.link_pattern = /(https?:\/\/)(www)?[A-Za-z0-9.\-@_~]+\.[A-Za-z]{2,}(:[0-9]{2,5})?(\/[A-Za-z0-9\/_\-.~?&=]*)*/

I'm rephrasing my question for clarification (it's isn't the styles I'm worried about, that already works) Is there a way for me to have real text (not placeholder) 'http://' in my input field and still have the link pattern validation 我正在重新说明我的问题以进行澄清(这不是我担心的样式,它已经起作用了) 我是否可以在输入字段中输入真实的文本(而非占位符)“ http://”,并且仍然具有链接模式验证

When the user sees the input box, i want the text 'http://' to be there (right now it's just a placeholder) so they don't have to type it themselves. 当用户看到输入框时,我希望文本“ http://”在那里(现在它只是一个占位符),这样他们就不必自己键入它。

You can try: 你可以试试:

ng-class="{invalid: (step3Form.button2link.$dirty && step3Form.button2link.$error.pattern)}" ng-class =“ {无效:(step3Form.button2link。$ dirty && step3Form.button2link。$ error.pattern)}”

I have created a plunker which will help you. 我创建了一个可以帮助您的矮子。

http://plnkr.co/edit/GVAdjcjcYczmcmzoCqoF http://plnkr.co/edit/GVAdjcjcYczmcmzoCqoF

<form role="form" name="signUpForm" class="form-horizontal">
  <div class="form-group">
      <label for="url" class="col-sm-4 control-label">URL</label>

      <div class="col-sm-8">
          <input type="text" class="form-control" name="url" id="url" placeholder="Enter last name"
                 ng-model="user.lastName" required="true" ng-pattern="/(https?:\/\/)(www)?[A-Za-z0-9.\-@_~]+\.[A-Za-z]{2,}(:[0-9]{2,5})?(\/[A-Za-z0-9\/_\-.~?&=]*)*/">

          <span ng-show="signUpForm.url.$dirty && signUpForm.url.$error.required">
              <small class="text-danger">Please enter valid URL.</small>
          </span>
          <span ng-show="signUpForm.url.$dirty && signUpForm.url.$error.pattern">
              <small class="text-danger">URL should have 2 to 25 characters.</small>
          </span>
      </div>
  </div>
</form>

http://jsfiddle.net/KGd8K/839/ http://jsfiddle.net/KGd8K/839/

<div ng-app ng-controller="formCtrl">
    <form name="myForm" ng-submit="onSubmit()">
        <input type="number" ng-model="price" name="price_field" ng-pattern="/^[0-9]{1,7}$/" required /> <span ng-show="{invalid: (myForm.price_field.$dirty && myForm.price_field.$error.pattern)}">Not a valid number!</span>
        <input type="submit" value="submit" />
    </form>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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