jquery mobile - Validate elements outside a form -
i have elements (input, textarea, controlgroup) , submit button outside of form element...
can validate elements (client-side)? instance, check if required , show message error if empty. can use jquery validation plugin that? how?
here sample of code until now:
<div id="formcontainer">      <div data-role="fieldcontain">         <fieldset data-role="controlgroup">             <legend>user:</legend>             <div id="userscontainer">                <input type="radio" name="radio-choice-1" id="radio-choice-1" value="1" />                <label for="radio-choice-1">user1</label>                <input type="radio" name="radio-choice-1" id="radio-choice-2" value="2" />                <label for="radio-choice-2">user2</label>             </div>         </fieldset>     </div>      <div data-role="fieldcontain" class="ui-hide-label">         <label for="subjectmessage">subject:</label>         <input type="text" name="subjectmessage" id="subjectmessage" value="" placeholder="subject" />     </div>      <div data-role="fieldcontain" class="ui-hide-label">         <label for="bodymessage">body:</label>         <textarea name="bodymessage" id="bodymessage" placeholder="body" rows="8"></textarea>     </div>      <input id="sendmessagebtn" type="submit" value="send" /> </div>  <script type="text/javascript">      $(document).one("pageinit", function () {          $("#sendmessagebtn").on("click", function () {              var userid= $('#formcontainer').find("#userscontainer :radio:checked").val();             var subject = $('#formcontainer').find("#subjectmessage").val();             var body = $('#formcontainer').find("#bodymessage").val();              //send data server...         });      });  </script>      
yes, there 2 approaches.
you don't use server side helpers generate markup , have hardcoded entire markup (as shown in question). bad if decide go approach have add corresponding
data-*attributes input fields unobtrusive validation framework using. it's possible use unobtrusive validation framework outside asp.net mvc, example in php application assuming input fields contain data-* attributes. said in previous sentece:outside asp.net mvc applicationbegs question:why using asp.net mvc in case if not take advantage of it?. recommend scraping approach , @ second possibility.you use server side helpers generate input fields such html.editorfor, html.textareafor, html.dropdownlistfor, ... helpers take care of generating proper
data-*attributes under 1 condition: elements inside form. not case, cheat adding following line top of view trick them thinking inside form:@{ this.viewcontext.formcontext = new formcontext(); }
Comments
Post a Comment