angularjs - 检查 Angular js中的字符串是否以字母开头?

标签 angularjs validation modal-dialog angular-filters

I am working with modal form. I have to make sure that entered string starts with letter otherwise i have show error message.

<section class="col-md-12">  
 <div data-ng-controller="coursesCtrl">
    <script type="text/ng-template" id="add_course.html">
        <div class="modal-header">
            <h3>Create Course</h3>
        </div>
        <div class="modal-body">
          <form name="course_form" role="form" class="form-horizontal form-validation" novalidate data-ng-submit="ok(course_form.$valid, courses.single)">
            <fieldset>
              <div class="form-group"  ng-class="{'has-error' : course_form.title.$invalid && (!course_form.title.$pristine || submitted) }">
                <div class="col-lg-3 col-md-3 col-sm-3">
                  <label for="title">Title</label>
                </div>
                <div class="col-lg-9 col-md-9 col-sm-9">
                  <input type="text"
                   class="form-control"
                   id="title"
                   placeholder="Title of the course"
                   data-ng-model="courses.single.title"
                   required name="title"
                   ng-Maxlength="45"
                   >
                </div>

                <div class="help-block col-lg-offset-3 col-sm-9 col-md-offset-3 col-md-9" ng-show="course_form.title.$invalid && (!course_form.title.$pristine || submitted)"> Title is required or it is taking more than 45 characters. 
                </div>                     
              </div>

            </fieldset>
          <div class="modal-footer">
            <button class="btn btn-default" id="create_course">Create</button>
            <button class="btn btn-default" ng-click="cancel()">Close</button>
          </div>
        </form>    
      </div>
    </script>   
     <button class="btn btn-primary pull-right"ng-click="open('lg')"><span class="glyphicon glyphicon-plus"></span> Create Course</button>   
</section>

How can i validate the title field. Could any one know how to solve it. Please let me know. Thanks in advance

最佳答案

听起来像是正则表达式的典型任务。

您可以使用以下命令检查 Controller 内的模型:

courses.single.title.match(/^[A-z].*/)

我还将其包装在一个条件中,以便空输入不会引发验证错误。

关于angularjs - 检查 Angular js中的字符串是否以字母开头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25121406/

相关文章:

angularjs - 如果在 STS 之外更改,Spring boot web main/resources/static 不会重新加载

javascript - 获取 ng-repeat 之外的值

Javascript/jQuery - 浮点验证?

jquery - 在 jquery 对话框窗口中偏离中心 x

javascript - 将新对象传递给 Angular 绑定(bind)时出现无限摘要错误

AngularJS - 指令 - 以下语法背后的原因是什么?

php - 滚动时,Twitter Bootstrap Modal 窗口不会保持在屏幕中央

javascript - 模态隐藏不起作用

javascript - 检查javascript中的文本框是否为空

javascript - javascript 与 html5 中的表单验证