I have following html
form. There will be many business validations – and if validation fail an "error" attribute
will be set on the element.
In the following form, I am returning a flag to see whether the validation is failed. How to make a common function to find all elements that has "error" attribute, and alert the value of "errDesc" attribute?
Note: I am looking for a JavaScript
solution without using other libraries.
Note: My client browsers does not support HTML5
<html>
<head>
<script type="text/javascript">
function loadMessage(frm)
{
alert("Page Loaded");
}
function CustomSetError(element, msg) {
alert("calledSUB");
var isError = false;
//In productioon code, error will be set based on business validation
try {
//Set error
element.setAttribute("error", "true");
element.setAttribute("errDesc", msg);
isError = true;
}
catch (e) { }
return isError;
}
function ValidateForm(frm) {
alert("validate");
var isErrorPresent = CustomSetError(frm.txtBillAddressText, "TestMessage");
if (isErrorPresent) {
return false;
}
}
</script>
</head>
<body bgcolor="#E5DBE2" topmargin="0" leftmargin="0" marginheight="0" marginwidth="0" onload="loadMessage(frmPOHeader)">
<form name="frmPOHeader" method="post" id="frmPOHeader">
<input name="txtBillAddressText" class="ArrowSmall" type="text" readonly="readonly" id="txtBillAddressText" fieldname="Bill To" />
<input name="hidBillToBuyerAddressCD" type="hidden" id="hidBillToBuyerAddressCD" />
<input type="submit" name="btnSubmit" value="Submit" onclick="return ValidateForm(frmPOHeader);" language="javascript" id="btnSubmit" class="ButtonSmallBlue" />
<br /><br />
<input name="txtDummy" class="ArrowSmall" type="text" readonly="readonly" id="txtDummy" fieldname="Dummy" />
</form>
</body>
</html>