Function to validate name for numbers and special characters

Solution

In Jquery

        var ValidateName= function (usr)
        {
            var regexp = new RegExp('^[A-Za-z]+$');
            var check = $(usr).val();
            if (!regexp.test(check)) {

                alert('Numbers and Special Characters are not allowed');
                $('#UserRole').css('border-color', 'red');
                return false;

            }
            else
            {
                $('#UserRole').css('border-color', 'green');
                return true;
            }

           
          
        }

HTML

<div class="col-sm-4 tb-font">
                                    @Html.Label("Role", "User Role", new { style = "width:140px; margin-bottom:5px; margin-top:5px; float: left;" })
                                    @Html.TextBox("UserRole", null, new { @class = "form-control text-box", @onchange = "ValidateName(this)" })
                                </div>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a free website or blog at WordPress.com.

Up ↑

Create your website at WordPress.com
Get started
%d bloggers like this: