2

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 i have two textboxes in my 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 is working but for firstname its not..What im doing wrong?

0

1 Answer 1

5

You don't need quotes around regex of firstname.

var firstname = "/^[A-Za-z]+$/"; // string
//              ^             ^

Use:

var firstname = /^[A-Za-z]+$/; // `regex`
Sign up to request clarification or add additional context in comments.

1 Comment

Thats it...tnx...i cant accept your answer for 8 more minutes but i will :)

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.