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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.