I have a page block table with 5 columns - First Name, Last Name, email and phone number. 4 of them are apex:input component and one remove command button. And i can add new rows on click of a "Add Row" button and remove row by clicking "Remove button".
How can i validate these input fields?
One way i found was using javascript, where if i know the id of the apex component say
<apex:page id="page">
<apex:form id="form">
<apex:inputText value="{!a}" id="aid" html-pattern="[a-z]+"/>
<apex:commandButton value="Click" onclick="return validate()"/>
</apex:form>
<apex:page>
On click of a button i call a javascript function validate(). which is as follows-
function validate(){
if(document.getElementById('{!$Component.page.form.aid}').value==''){
document.getElementById('{!$Component.page.form.aid}').style.border = "solid 1px red";
return false;
}
else{
return true;
}
}
In this case i would know the exact id of the apex component so that i can validate using javascript, but what happens in page block table is that id will be different for each row and it is dynamically generated. So any idea on how to add validation to that? ex: id of 1st row 1st column would be -
page:form:1:hhm1FirstName
and 2nd row 1st column would be
page:form:2:hhm1FirstName