javascript - Validating form input fields while typing using jQuery? -
i have script :
<script> jquery(document).ready(function($) { var firstname = "/^[a-za-z]+$/"; var email = /^[ ]*([^@@\s]+)@@((?:[-a-z0-9]+\.)+[a-z]{2,})[ ]*$/i; jquery('input#firstname').bind('input propertychange', function() { if (firstname.test(jquery(this).val())) { jquery(this).css({ 'background': '#c2d699' }); } else { jquery(this).css({ 'background': '#ffc2c2' }); } }); jquery('input#email').bind('input propertychange', function() { if (email.test(jquery(this).val())) { jquery(this).css({ 'background': '#c2d699' }); } else { jquery(this).css({ 'background': '#ffc2c2' }); } }); }); </script>
and in view have 2 textboxes in view:
@html.textboxfor(m => m.register.firstname, new { id = "firstname", @class = "form-control", @maxlength = "16" }) @html.textboxfor(m => m.register.email, new { id = "email", @class = "form-control", @type = "email" })
for email validation working firstname not..what im doing wrong?
you don't need quotes around regex
of firstname.
var firstname = "/^[a-za-z]+$/"; // string // ^ ^
use:
var firstname = /^[a-za-z]+$/; // `regex`
Comments
Post a Comment