0

I just want to Validate my HTML text box required field validator in MVC 5, My View,its very important to use the onclick event,please guide,I have already wasted 3 hours on this I know its very simply, I tried this link but it didn't worked

 @using (Html.BeginForm("Search", "Controller"))
       {
       <input id="txtsearch" type="text" name="searchbox" />
       <input id="Button1"  type="submit" value="Search" 
       onclick="location.href='@Url.Action("Search","Video")'" />
       }

1 Answer 1

1

You can use the required/title attributes (http://www.w3schools.com/tags/att_input_required.asp)

<form action="@Url.Action("Search","Video")" method="POST">
  <input id="txtsearch" type="text" name="searchbox" required title="Your validation message here." />
  <input id="Button1"  type="submit" value="Search" />
</form>

Custom HTML5 Form Validation: http://www.girliemac.com/blog/2012/11/21/html5-form-validation/

Browsers compatibility http://caniuse.com/#search=required

Sign up to request clarification or add additional context in comments.

4 Comments

Hi, your solutions works fine but now as I am using form Action my hit is not going to the Action Method,I think it has connection with RouteConfig my config is routes.MapRoute( name: "videosearch", url: "video/search", defaults: new { controller = "video", action = "search" } );
Sorry for my mistake, you also need to add method = "POST" in the form. Edited my answer.
So Sorry, I thaught that by giving a positive comments count but where is the option to mark this as solution.
Hi, I am trying to to Vote up but the intellisense says Vote up requries 15 reputation and I don't have it,I seriously want to accept you answer as it helped me a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.