0

I am making a simple html page with validation by angularjs.. I have taken the html file with angularjs by this process

  <title>Student Registration Form</title>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.4/angular.min.js"></script>

while I am giving the validation in this way

<body ng-app>

and then I am giving the validation in the text area like this code

 <input type="text" name="First_Name" maxlength="30" ng-pattern="/^[a-zA-Z\s]*$/"/> 

this ng pattern refers that only alphabets and blank space could be entered

but the error is the validation is not happening.

0

2 Answers 2

1

There is no validation because input field has no model bound, so no data to validate. Add ngModel directive and it should work:

<input type="text" name="First_Name" ng-model="user.firstName" maxlength="30" ng-pattern="/^[a-zA-Z\s]*$/" />
Sign up to request clarification or add additional context in comments.

2 Comments

If you want validation or use data later in controller, then yes.
You will be able to type of course. ngPattern is not the way to prevent incorrect input, it's about validating it. So if you want to block input you need to use ngKeypress and prevent event default if character is not allowed.
0

You can output the validation state by reading .$valid of the field

<body ng-app  >

<div >
  <form name="myForm">
    <label>
       User name:
       <input type="text" name="userName" ng-model="userName"  ng-pattern="/^[a-zA-Z\s]*$/" >
    </label>
      <div>
user: {{userName}}
   </div>

  </form>
  <hr>

  <tt>myForm.userName.$valid = {{myForm.userName.$valid}}</tt><br/>

</div>
</body>

http://plnkr.co/edit/lml1eBqEkvCBpz2kb51Z?p=preview

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.