Pregunta

I have a control input type is text that will accept id which will enter by user. When user doesn't enter any id and when he will click on button for getting details I have to show message that "please enter id in textbox". All these things has beeen design in mvc3. Now please help to us that how we will do it ? I am new on mvc3.

Update

This is my HTML Code:

<div class="centerize">
   <input type="text" name="entryid" value="" placeholder="Enter valid id" style="height:30px; text-align: center"/>
</div> 
<div class="centerize" style="height:20px"></div>
<div class="centerize">
  <input type="submit" value="Submit" style="height: 30px; width: 100px"/>
</div> 
<div class="centerize">
   @Html.ValidationSummary()
</div>
¿Fue útil?

Solución

if you do want to use Javascript validation:

 @Html.TextBoxFor(m=>m.userid,new {id="userid"})

JS:

$(#submit).click(function()
{
    var data=$("#userid").val();

     if(data.length==0)
     {
       alert("Please enter user id");
      return false;  
     }
});

If you want to use data annotations for your model for validation just use [Required] attribute for your

field userid

Otros consejos

If you wanna do it on server side, add a Required data Annotation to the field:

[Required]
public string UserId{get;set;}

And, if you wanna do it on client-side, use this script;

<script>
$('input:submit').click(function()
{
    var $entryId=$('input[name="entryid"]').val();    
     if($entryId.trim()=="")
     {
       alert("Entry Id is required!");
      return false;  
     }
});
</script>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top