AngularJS - 仅限字母,不进行空格验证

时间:2018-06-13 07:39:26

标签: javascript angularjs

我想用字母验证用户名,不会输入空格...我在这种情况下使用ng-patterns作为ng-pattern =' / ^ [a-zA-Z \ d-_] + $ /' ..但ng-pattern不工作。我是角度js的新手..任何角度js都可以帮助,请。必须要求usename,只有leetes会 被接受没有数字,也没有空间



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<head>
<style type="text/css">
  .container {
  width: 970px;
  padding-top: 10px;
  margin: 0 auto;
}

.pure-form input.ng-dirty.ng-invalid {
  border-color: #e9322d;
}

.field-message {
  display: inline-block;
  color: #e9322d;
  font-size: 90%;
  margin-left: 0.5em;
}

[ng\:cloak],
[ng-cloak],
[data-ng-cloak],
[x-ng-cloak],
.ng-cloak,
.x-ng-cloak {
  display: none !important;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-messages.min.js"></script>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css"></link>
 </head>
<body> 
<div class="container" id="demoApp">
  <form class="pure-form pure-form-aligned" name="frm" method="post" novalidate autocomplete="off">
    <fieldset>
      <div class="pure-control-group">
        <label>Username</label>
        <input name="username" ng-model="user.username" value="Debasish" type="text" placeholder="Username"  required  ng-pattern='/^[a-zA-Z\d\-\_]+$/' >
        <div class="field-message" ng-messages="frm.username.$error" ng-if='frm.username.$dirty' ng-cloak>
          <div ng-message="required">Username is required</div>
          <div ng-pattern="required">Letters Only and No whitespace</div>
        </div> 
      </div>

      <div class="pure-control-group">
        <label>Password</label>
        <input name="password" ng-model="user.password" type="password" placeholder="Password" required ng-minlength="6" ng-maxlength="10">
        <div class="field-message" ng-messages="frm.password.$error" ng-if='frm.password.$dirty' ng-cloak>
          <div ng-message="required">Password is required</div>
          <div ng-message="minlength">Password must have minimum 6 characters</div>
          <div ng-message="maxlength">Password must have maximum 10 characters</div>
        </div>
      </div>

      <div class="pure-control-group">
        <label>Email Address</label>
        <input name="email" ng-model="user.email" type="email" placeholder="Email Address" required>
        <div class="field-message" ng-messages="frm.email.$error" ng-if='frm.email.$dirty' ng-cloak>
          <div ng-message="required">Email is required</div>
          <div ng-message="email">Must be a valid email</div>
        </div>
      </div>
      <div class="pure-controls">
        <label class="pure-checkbox">
                      <input name="conditions" ng-model="conditions" type="checkbox">
                      I've read the terms and conditions
                  </label>
        <button type="submit" class="pure-button pure-button-primary" ng-disabled="frm.$invalid || !conditions">Submit</button>
      </div>
    </fieldset>
  </form>
</div>
<script type="text/javascript">
angular.module('DemoApp', ['ngMessages']);
angular.bootstrap(document.getElementById('demoApp'), ['DemoApp']);
</script>
</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

要在用户的输入与模式匹配时显示错误,您必须使用ng-message属性。
使用消息在html元素上设置ng-message="pattern"而不是ng-pattern="required"

&#13;
&#13;
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<head>
<style type="text/css">
  .container {
  width: 970px;
  padding-top: 10px;
  margin: 0 auto;
}

.pure-form input.ng-dirty.ng-invalid {
  border-color: #e9322d;
}

.field-message {
  display: inline-block;
  color: #e9322d;
  font-size: 90%;
  margin-left: 0.5em;
}

[ng\:cloak],
[ng-cloak],
[data-ng-cloak],
[x-ng-cloak],
.ng-cloak,
.x-ng-cloak {
  display: none !important;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-messages.min.js"></script>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css"></link>
 </head>
<body> 
<div class="container" id="demoApp">
  <form class="pure-form pure-form-aligned" name="frm" method="post" novalidate autocomplete="off">
    <fieldset>
      <div class="pure-control-group">
        <label>Username</label>
        <input name="username" ng-model="user.username" type="text" placeholder="Username"  required  ng-pattern='/^[a-zA-Z\d\-\_]+$/' >
        <div class="field-message" ng-messages="frm.username.$error" ng-if='frm.username.$dirty' ng-cloak>
          <div ng-message="required">Username is required</div>
          <div ng-message="pattern">Letters Only and No whitespace</div>
        </div> 
      </div>

      <div class="pure-control-group">
        <label>Password</label>
        <input name="password" ng-model="user.password" type="password" placeholder="Password" required ng-minlength="6" ng-maxlength="10">
        <div class="field-message" ng-messages="frm.password.$error" ng-if='frm.password.$dirty' ng-cloak>
          <div ng-message="required">Password is required</div>
          <div ng-message="minlength">Password must have minimum 6 characters</div>
          <div ng-message="maxlength">Password must have maximum 10 characters</div>
        </div>
      </div>

      <div class="pure-control-group">
        <label>Email Address</label>
        <input name="email" ng-model="user.email" type="email" placeholder="Email Address" required>
        <div class="field-message" ng-messages="frm.email.$error" ng-if='frm.email.$dirty' ng-cloak>
          <div ng-message="required">Email is required</div>
          <div ng-message="email">Must be a valid email</div>
        </div>
      </div>
      <div class="pure-controls">
        <label class="pure-checkbox">
                      <input name="conditions" ng-model="conditions" type="checkbox">
                      I've read the terms and conditions
                  </label>
        <button type="submit" class="pure-button pure-button-primary" ng-disabled="frm.$invalid || !conditions">Submit</button>
      </div>
    </fieldset>
  </form>
</div>
<script type="text/javascript">
angular.module('DemoApp', ['ngMessages']);
angular.bootstrap(document.getElementById('demoApp'), ['DemoApp']);
</script>
</body>
</html>
&#13;
&#13;
&#13;