javascript - Form validation using AngularJS not working -
i planning simple form validation using angularjs seems not working. here plunkr file: http://plnkr.co/edit/sepahszlfoflfb87uh8s.
i don't know why, seems not firing.
my html file:
<html> <head> <!-- css --> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" /> <style> body { padding-top:30px; } </style> <!-- js --> <script src="http://code.angularjs.org/1.2.6/angular.js"></script> <script src="app.js"></script> </head> <body ng-app="validationapp" ng-controller="maincontroller"> <div class="container"> <!-- page header --> <div class="page-header"> <h1>angularjs form validation</h1> </div> <!-- =================================================================== --> <!-- form ============================================================== --> <!-- =================================================================== --> <!-- pass in variable if our form valid or invalid --> <form name="regform" ng-submit="submitform(regform.$valid)" novalidate> <!-- name --> <div class="form-group item item-input" ng-class="{ 'has-error' : regform.name.$invalid && (regform.name.$dirty || submitted)}"> <input type="text" name="name" class="form-control" ng-model="user.name" placeholder="full name" ng-required="true"> <br/> <p ng-show="regform.name.$error.required && (regform.name.$dirty || submitted)" class="help-block">please provide full name</p> </div> <div class="form-group item item-input" ng-class="{ 'has-error' : regform.email.$invalid && (regform.email.$dirty || submitted)}"> <input type="email" name="email" class="form-control" ng-model="user.email" placeholder="email address" ng-required="true"> <p ng-show="regform.email.$error.required && (regform.email.$dirty || submitted)" class="help-block">email required.</p> <p ng-show="regform.email.$error.email && (regform.email.$dirty || submitted)" class="help-block">enter valid email.</p> </div> <div class="form-group item item-input" ng-class="{ 'has-error' : regform.contactno.$invalid && (regform.contactno.$dirty || submitted) }"> <input type="text" name="contactno" class="form-control" ng-model="user.contactno" placeholder="phone number" ng-pattern="^0[0-9]{2}[- ]?[0-9]{3} ?[0-9]{4,5}$" maxlength="11" ng-required="true"> <p ng-show="regform.contactno.$error.required && regform.contactno.$error.pattern && (regform.contactno.$dirty || submitted)" class="help-block">enter valid phone number.</p> </div> <div class="form-group item item-input" ng-class="{ 'has-error' : regform.org.$invalid && (regform.org.$dirty || submitted)}"> <input type="text" name="org" class="form-control" ng-model="user.org" placeholder="organization name" ng-required="true"> <p ng-show="regform.org.$error.required && (regform.org.$dirty || submitted)" class="help-block">please input name of organization</p> </div> <button type="submit" class="btn btn-primary">submit</button> </form> </div> </body> </html>
my js file:
var validationapp = angular.module('validationapp', []); // create angular controller validationapp.controller('maincontroller', function($scope) { // function submit form after validation has occurred $scope.submitform = function(isvalid) { // check make sure form valid if (isvalid) { alert('our form amazing'); } }; });
i need validate required fields have related input , not empty fields.
your ng-pattern
should ng-pattern="/^[0-9]{2}[- ]?[0-9]{3} ?[0-9]{4,5}$/"
instead of ng-pattern="^0[0-9]{2}[- ]?[0-9]{3} ?[0-9]{4,5}$"
regx pattern should use escaping character /
@ start & end of expression.
Comments
Post a Comment