Wednesday, March 19, 2014

A new way to validate email

Gone are the days of ugly, gaudy RegEx email validation.  Here is an example of a nice clean way to validate email with AngularJS:


@using (Html.BeginForm(null, null, FormMethod.Get, new { name = "recoverForm" }))
 {
       <fieldset>
              <div class="editor-label">
                     Email Address
              </div>

              <div class="input-group" ng-class="{'has-error': IsValid(recoverForm.email.$error)}">
                     <span class="input-group-addon">&#64;</span>
                     <input type="email" ng-model="email" name="email" class="form-control" placeholder="Enter Email Address" maxlength="255" required />
              </div>
              <br /><br />
              <p>
                     <a href="javascript:void(0);" class="btn btn-primary" ng-click="RecoverPassword()">Recover Password</a>
              </p>
       </fieldset>

 }

 <script>
 ....

 $scope.IsValid = function (input) {
        if (input.required) {
            return true;
        }

        if (input.email) {
            return true;
        }
        return false;
    }

 ....
 </script>



No comments:

Post a Comment