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>

4 Answers 4

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>
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

what is the output you see on screen? Can you share a working fiddle or snippet?
@creative check your browser's developer console, tell me if there is any error.
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.

Comments

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/

Comments

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.