html - javascript chalanging form elemts array validation -


hi have 1 html form.

<form name="modeladd" ethod="post" class="form label-inline" enctype="multipart/form-data" onsubmit="return check('modeladd');">             <?php  for($i=0;$i<$tot;$i++) { ?>                       <div class="field">                 <label for="connect_msg">connect message </label>                                                                                    <textarea rows="4" cols="50" name="connect_msg[<?php echo getfield($langs[$lan]);?>]" id="connect_msg[]"><?php echo getfield($row[$lan]); ?></textarea>                 </div>                               <div class="field">                 <label for="call_msg">call message :</label>                                                                 <textarea rows="4" cols="50" name="call_msg[<?php echo getfield($langs[$lan]);?>]" id="call_msg"><?php echo getfield($row[$lan]); ?></textarea>                 </div>                  <div class="field">                 <label for="stripline_msg">stripline message :</label>                                                               <textarea rows="4" cols="50" name="stripline_msg[<?php echo getfield($langs[$lan]);?>]" id="stripline_msg"><?php echo getfield($row[$lan]); ?></textarea>                 </div>             <div class="field">                     <label for="cost_msg">cost message :</label>                                                                 <textarea rows="4" cols="50" name="cost_msg[<?php echo getfield($langs[$lan])?>]" id="cost_msg"><?php echo getfield($row[$lan]); ?></textarea>                 </div>                           <?php } ?> <input type="submit" class="btn btn-grey" value="submit"> </form> 

here javascript validation

function check(form) {    var name = new array();    name =document.getelementbyid('connect_msg[]').value;     for(var i=0 ; i<=1; i++)    {        alert(name[i].value);     }    return true; } 

i not getting value both connect_msg field array. want validate both field value using javascript pls me

  1. the id cannot have [] symbols. please validate markup: http://validator.w3.org/
  2. the value returned element type string, can't array. can parse string create array.

eg. assuming following outputted markup:

<textarea id="connect_msg">a,b,c,d,e,f,g</textarea> 

you can in javascript:

var getconnectmsg = document.getelementbyid('connect_msg').value; var connectmsgarray = getconnectmsg.split(','); 

and connectmsgarray contain ['a','b','c','d','e','f','g'].

you can read more .split() here: https://developer.mozilla.org/en-us/docs/javascript/reference/global_objects/string/split

edit: since textarea in loop, it's more easier retrieve elements using class name attribute, , using jquery immensely.

so html output this:

<textarea class="connect_msg" id="connect_msg1">a,b,c,d,e,f,g</textarea> <textarea class="connect_msg" id="connect_msg2">h,i,j,k,l,m,n</textarea> <textarea class="connect_msg" id="connect_msg3">q,r,s,t,u,v</textarea>  var getconnectmsg = $('.connect_msg'); getconnectmsg.each(function(){     var getvalue = $(this).val();     var connectmsgarray = getvalue.split(','); }); 

in each iteration of .each(), value of getvalue be:

 a,b,c,d,e,f,g  h,i,j,k,l,m,n  q,r,s,t,u,v 

you can form validation set of fields during each iteration.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -