think_user
think_user

Reputation: 393

how to validate array input field using javascript

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>

Upvotes: 3

Views: 14730

Answers (4)

shankit
shankit

Reputation: 21

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/

Upvotes: 0

andrea_19920611
andrea_19920611

Reputation: 76

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>

Upvotes: 1

Rahul Patel
Rahul Patel

Reputation: 5246

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.

Upvotes: 0

gurvinder372
gurvinder372

Reputation: 68393

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;
}

Upvotes: 0

Related Questions