<script type="text/javascript">
function validate() {
if (document.form.price.value.trim() === "") {
alert("Please enter a price");
document.form.price.focus();
return false;
}
if (document.form.price.value !== "") {
if (! (/^\d*(?:\.\d{0,2})?$/.test(document.form.price.value))) {
alert("Please enter a valid price");
document.form.price.focus();
return false;
}
}
return true;
}
</script>
<form action="" method="post" name="form" id="form" onsubmit="return validate(this);">
<input name="price" type="text" class="r2" />
<input name="price2" type="text" class="r2" />
<input name="price3" type="text" class="r2" />
<input name="price4" type="text" class="r2" />
<input name="price5" type="text" class="r2" />
...more....
<input name="price50" type="text" class="r2" />
Current javascript code working fine to validate the name="price".
Question : How to make the code will working as global validation? Example can validate the price, price2, price3, price4, price5 etc.. with a single function. Let me know :)