Click here to Skip to main content
13,143,185 members (57,631 online)
Rate this:
Please Sign up or sign in to vote.
Hi friends,

I'm working on MVC3 project. I want to validate one textbox with two conditions.

1) Need not allow null strings to get insert into database.
2) Need not accept special characters and numbers.

Razor view for particular partial page is:

Model class is
[Required(ErrorMessage = "Required")]
        [RegularExpression(@"^[a-zA-Z]+", ErrorMessage = "Use letters only please")]
        public string roleShortDescription { get; set; }

How can i implement the validation?
Can i use jquery? if so, how?
Please help me !!!!

With regards,
Posted 16-Aug-12 23:20pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You need to validate on the server, any client side validation is easily hacked. You can add a keypress event handler to your textbox and validate as users type. The check for null strings you can do when you read the values in your viewmodel, just use ?? to replace null with string.Empty.

I was going to say that Razor has nothing to do with it, but I realised that is not true. A textbox in HTML is like this:

<input type="text"...

and then a key press event would be like this:

<input type="text" onkeypress="OnKeyPress(this)"...

where OnKeyPress is a javascript method, and this means your input control is passed in to it.

This[^] is some documentation on js events.

Here[^] is an example to allow only numeric key presses.

The problem you have is, when you use TextBoxFor, how do you get that code in there ? The answer is, an override of TextBoxFor that takes an anonymous collection of name value pairs which are then added as attributes to the element:

@Html.TextBoxFor(m => m.Product, new { @class = "normalcell", size = "38", maxlength = "255" })

This is from my own code, and it will emit this:

<input type="text" id="Product" class="normalcell" size="38" maxlength="255"/>

so, you would do this:

@Html.TextBoxFor(m=>m.shortDescription, new { @onkeypress="MyValidationMethod" })
rk_prabakar 17-Aug-12 6:25am
In razor engine, how to add event handler to particular text box?
Christian Graus 17-Aug-12 9:45am
I have added an explanation to my solution.
rk_prabakar 21-Aug-12 10:27am
thanks pal, but where should i write the code for "MyValidationMethod" function? I'm lacking there. I wrote this code for the above method, but i dont know where to write.So plz help me

el.onkeypress = KeyPressHandler;
function KeyPressHandler(event)
evt = (evt) ? evt : window.event
var charCode = (evt.which) ? evt.which : evt.keyCode
if ((charCode < 123 || charCode > 96 ) || (charCode < 90 || charCode > 64 ))
status = "Only alphabets allowed"
return false;
return true;
Christian Graus 21-Aug-12 10:43am
MyValidationMethod is just a javascript method which follows the format you just posted, and is visible to your webpage, directly or via a js file. So rename your KeyPressHandler or make KeyPressHandler the name in the attribute, and that's it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170915.1 | Last Updated 17 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100