I am implementing multiple input fields in a form element, So inside form may be one input field and more than one input field. So I want to implement one method for validation. For a phone number, email, age, date, etc. So in future may be formed has 15 input fields. So in that case on method should handle all input fields.
Below is the sample code:
<form onsubmit="return false">
<div>
<label for="">Name</label>
<input type="text" name="fname" id="fname" onkeyup=handleInput(this)>
</div>
<div>
<label for="">Middle Name</label>
<input type="text" name="mname" id="mname" onkeyup=handleInput(this)>
</div>
<div>
<label for="">Age</label>
<input type="number" name="age" id="age" onkeyup=handleInput(this)>
</div>
<div>
<label for="">Email</label>
<input type="text" name="email" id="email" onkeyup=handleInput(this)>
</div>
<div>
<label for="">Mobile</label>
<input type="number" name="mobile" id="number" onkeyup=handleInput(this)>
</div>
<div>
<button onclick=submitData()>Submit</button>
</div>
</form>
<script>
var data={ };
function handleInput(e){
data[e.name] = e.value;
}
function submitData(){
console.log(data); //return object
}
</script>
DEMO https://jsfiddle.net/varunPes/2fdoxanu/5/
Is it possible to handle validation through one function? If anyone has any idea, Please share, Thank you in advance.