Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
@Html.TextBoxFor(m=>m.shortDescription)

Model class is
C#
[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,
R.K.PRABAKAR
Posted

1 solution

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:

C#
@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:


C#
@Html.TextBoxFor(m=>m.shortDescription, new { @onkeypress="MyValidationMethod" })
 
Share this answer
 
v2
Comments
♥…ЯҠ…♥ 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.
♥…ЯҠ…♥ 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900