3

How to validate array input field?Please help me to solve this issue.

<form action="" method="post" name="frmPayPal1" id="frmPayPal1" onsubmit="return validateForm()"/>
 <input type='text' name='txt_jobid[]' id='txt_jobid' >
  <input type="submit" value="submit"/>
  </form>

<script>

 function validate(job)
 {
 if(job.elements['txt_jobid[]'].length == 0)
{
    alert(" Please Enter Value!");  
    return false;
}   

}
 </script>
think_user
  • 393
  • 2
  • 8
  • 24

4 Answers4

1

Maybe this...

<form action="" method="post" name="frmPayPal1" id="frmPayPal1">
    <input type="text" name="txt_jobid[]" id="txt_jobid">
    <input type="submit" value="submit">
</form>

<script>
$(document).ready(function(){
    $('#frmPayPal1').on('submit', function(){
        var lngtxt=($(this).find('input[name="txt_jobid[]"]').val()).length;
        console.log(lngtxt);
        if (lngtxt==0){
            alert('please enter value');
            return false;
        }else{
            //submit
        }

    });
});
</script>
0

You need to pass this

onsubmit="return validateForm(this)"

Also, return true if validation succeeds

function validate(job)
{
  if(job.elements['txt_jobid[]'].length == 0)
  {
    alert(" Please Enter Value!");  
    return false;
  }   
  return true;
}
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

function validateForm(){
  if((document.getElementById("txt_jobid").value).length == 0){
    alert(" Please Enter Value!");  
      return false;
  }   
} 
<form action="" method="post" name="frmPayPal1" id="frmPayPal1" onsubmit="return validateForm()">
  <input type='text' name='txt_jobid[]' id='txt_jobid'>
  <input type="submit" value="submit"/>
</form>

Please let me know if you have any query.

Rahul Patel
  • 5,248
  • 2
  • 14
  • 26
0

here the solution of input type array. I have already post the solution on stackoverflow . please the below link.

https://stackoverflow.com/a/48092147/8189107

here the js fiddle link. you can see it's working here

https://jsfiddle.net/q0d76aqx/42/
shankit
  • 11
  • 6