asp.net mvc4

Hi Guys ,

I'm working on asp.net mvc4 web application and i'm trying to restrict special character from input fields.

for example I have this textbox:

Supplier Contact:  @Html.TextBoxFor(m => m.POTitle, new { @class = "form-control", @placeholder = "Type Name Or Email" })

Open in new window


I would like the restrict like character - /^\s*[a-zA-Z0-9,\s]+\s*$/ when users type in.

I found some jquery something like that:
$(document).ready(function () {
    var charReg = /^\s*[a-zA-Z0-9,\s]+\s*$/;
    $('.keyup-char').keyup(function () {
        $('span.error-keyup-1').hide();
        var inputVal = $(this).val();

        if (!charReg.test(inputVal)) {
            $(this).parent().find(".warning").show();
        } else {
            $(this).parent().find(".warning").hide();
        }

    });
});

Open in new window


but i dont know how to use it in my case.
LVL 1
Moti Mashiah.NET DeveloperAsked:
Who is Participating?
 
Miguel OzSoftware EngineerCommented:
Just add it to your regex expression as shown below:
var charReg = /^\s*[a-zA-Z0-9,@\s]+\s*$/;

You can test expression using this free tool
0
 
Moti Mashiah.NET DeveloperAuthor Commented:
Hi Guys ,

I figured it out how to do it.

Here is the solution:

<div>
                                Supplier Contact:  @Html.TextBoxFor(m => m.POTitle, new { @class = "form-control", @placeholder = "Type Name Or Email" })
                                <input type="hidden" id="POTitle" name="po" class="form-control" value="@Model.POTitle;" />
                                <span class="warningchar">characters only.</span>
                            </div>

Open in new window



 var charReg = /^\s*[a-zA-Z0-9,\s]+\s*$/;
            $('.form-control').keyup(function () {
                var inputVal = $(this).val();

                if (!charReg.test(inputVal)) {
                    $(this).parent().find(".warningchar").show();
                } else {
                    $(this).parent().find(".warningchar").hide();
                }

            });

Open in new window


Now I left with one question. How do I allow @ in the textbox. I would like to restrict all special character but @.

For now it is also restrict the @.

Thanks.
0
 
Moti Mashiah.NET DeveloperAuthor Commented:
Solved
0
 
Moti Mashiah.NET DeveloperAuthor Commented:
Sorry , i thought it solved my problem but it is not.

I'm using asp.net and this @ is like connection to C# from the view and it gives me error.

please advice how can i do it differently.

 
var charReg = /^\s*[a-zA-Z0-9,\s]+\s*$/;
            $('.form-control').keyup(function () {
                var inputVal = $(this).val();

                if (!charReg.test(inputVal)) {
                    $(this).parent().find(".warningchar").show();
                } else {
                    $(this).parent().find(".warningchar").hide();
                }

            });

Open in new window

0
 
Miguel OzSoftware EngineerCommented:
Please post error details and provide which area (line number) the error is fired.
If possible provide a sample project with a controller and view that exhibits the issue. I use VS2010 or VS2013.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.