Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: validation MVC3 Razor
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
[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 17-Aug-12 0:20am

1 solution

Rate this: bad
good
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" })
  Permalink  
v2
Comments
rk_prabakar at 17-Aug-12 6:25am
   
In razor engine, how to add event handler to particular text box?
Christian Graus at 17-Aug-12 9:45am
   
I have added an explanation to my solution.
rk_prabakar at 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 at 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
0 OriginalGriff 406
1 /\jmot 180
2 Suraj Sahoo | Coding Passion 170
3 Afzaal Ahmad Zeeshan 159
4 BillWoodruff 154
0 OriginalGriff 8,344
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,634
3 Maciej Los 5,024
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 17 Aug 2012
Copyright © CodeProject, 1999-2014
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