1

A simple angularjs form validation is not working in any of the browsers. I am using Netbeans 8.0.2 IDE. I am not sure what is the problem. Is there any thing wrong in the code?

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Number only</title>
        <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
        <script>
            angular.module('myApp', []).controller("numOnlyRegex", function ($scope)
            {
                $scope.numOnlyRegex = /^\d+$/;
            });
        </script>

    </head>
    <body>
        <div ng-app="myApp" ng-controller="numOnlyRegex">
            <form name="myForm">
                Pin:<input type="text" ng-model="pin" ng-pattern="numOnlyRegex" name="pin" /><br/>
                <input type="Submit" value="Submit" />
            </form>
        </div>
    </body>
</html>
11
  • 1
    Hey i checked it working perfectly :) Here is plunker "plnkr.co/edit/Fni3pDdOvYA8B7IpRCzD?p=preview" Commented Jun 12, 2015 at 6:33
  • But I am able to type letters in the input field in the plunker Commented Jun 12, 2015 at 6:38
  • Or atleast before submitting I want to check if the user typed in is only numbers or throw error message. Also the error message in the span tag keeps displaying even after entering correct value Commented Jun 12, 2015 at 6:39
  • This will validate in the way that it will set the form validity to invalid whne you type in letters in the input field but it won't stop you from typing in letters. If you want to do that then you'd need to intercept a keyup event for example where you can check for every character if it is a number Commented Jun 12, 2015 at 6:40
  • 1
    @kittu No, but you can prevent him from doing so, for example by calling a method through ng-submit and checking for the forms validity there Commented Jun 12, 2015 at 6:53

1 Answer 1

4

Your code is fine. You just need to disable the submit button on invalid input!

 <input type="Submit" ng-disabled="myForm.$invalid"  value="Submit" />

Here's the plunkr

Hope it helps!

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks NLN and also Along with disabling the button,how can I show a error message below input text box if the user enters letters?
@kittu add a span containing an error message that you will conditionally show (with ng-show for example) when the input control is $invalid
@kittu: You just need to add a span for that and use ng-show directive to display error message.
@kittu: Here's the updated plunkr
The form is not getting disabled for the digit only input(i.e. pincode/zipcode). Please check the plunkr plnkr.co/edit/nMyfgV9kfyza1ErcpF7N?p=preview
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.