Site hosted by Angelfire.com: Build your free website today!
   
Validation Code Sample

 
The following code is an example of some of the validation that was implemented in the Job Order form.  Please note the comments in red for an explanation.
 
/**
* Perform the validateFields method.
*/

public void validateFields() throws com.ibm.ivj.eab.dab.DAException, template.model.ValExc{

//check for blank entry

//here we get the textfield object, get the text, trim it and set it equal to something.
if(gettxtJobTitle().getText().trim().equals("")) 
{
//set focus to jobtitle textbox
gettxtJobTitle().requestFocus(); 
throw new template.model.ValExc("Please enter a Job Title.");
}

else if (gettxtJobTitle().getText().length() > 20) {
gettxtJobTitle().requestFocus();
throw new template.model.ValExc("Job Title must be no more than 20 characters.");
}

else if(gettxtSalaryMax().getText().trim().equals(""))
{
//set focus to salary max textbox
gettxtSalaryMax().requestFocus();
throw new template.model.ValExc("Please enter a maximum salary."); 
}

else if(gettxtSalaryMin().getText().trim().equals(""))
{
//set focus to salary min textbox
gettxtSalaryMin().requestFocus();
throw new template.model.ValExc("Please enter a minimum salary."); 
}

else if(gettxtDateOpened().getText().trim().equals(""))
{
//set focus to date opened textbox
gettxtDateOpened().requestFocus();
throw new template.model.ValExc("Please enter an opening date."); 
}

else if(gettxtDateClosed().getText().trim().equals(""))
{
//set focus to date closed textbox
gettxtDateClosed().requestFocus();
throw new template.model.ValExc("Please enter a closing date."); 
}

//*****************************************
//Length val for date closed

if(gettxtDateClosed().getText().trim().length() != 10) {

gettxtDateClosed().requestFocus();
throw new template.model.ValExc("Closing Date format must be yyyy-mm-dd");
}
//Length val for date opened
if(gettxtDateOpened().getText().trim().length() != 10) {
gettxtDateOpened().requestFocus();
throw new template.model.ValExc("Opening Date format must be yyyy-mm-dd");
}

//No character validation in date closed
String year = gettxtDateClosed().getText().trim().substring(0,3);
String month = gettxtDateClosed().getText().trim().substring(5,6);
String day = gettxtDateClosed().getText().trim().substring(8,9);
try{
Long.valueOf(year);
Long.valueOf(month);
Long.valueOf(day);
}
catch(NumberFormatException e){

gettxtDateClosed().requestFocus();
throw new template.model.ValExc("The 'Date Closed' field must cannot contain characters.");
}

//No character validation in date opened
String year2 = gettxtDateOpened().getText().trim().substring(0,3);
String month2 = gettxtDateOpened().getText().trim().substring(5,6);
String day2 = gettxtDateOpened().getText().trim().substring(8,9);
try{
Long.valueOf(year2);
Long.valueOf(month2);
Long.valueOf(day2);
}
catch(NumberFormatException e){

gettxtDateOpened().requestFocus();
throw new template.model.ValExc("The 'Date Opened' field cannot contain characters.");
}

//formatting for date
gettxtDateClosed().getText().trim().length();
char check42 = gettxtDateClosed().getText().trim().charAt(4);
char check72 = gettxtDateClosed().getText().trim().charAt(7);
String chk42 = String.valueOf(check42);
String chk72 = String.valueOf(check72);
if(!(chk42.equals("-"))) {

gettxtDateClosed().requestFocus();
throw new template.model.ValExc("Date Closed format must be yyyy-mm-dd");
}
if(!(chk72.equals("-"))) {
gettxtDateClosed().requestFocus();
throw new template.model.ValExc("Date Closed format must be yyyy-mm-dd");
}

//formatting for date
gettxtDateOpened().getText().trim().length();
char check4 = gettxtDateOpened().getText().trim().charAt(4);
char check7 = gettxtDateOpened().getText().trim().charAt(7);
String chk4 = String.valueOf(check4);
String chk7 = String.valueOf(check7);
if(!(chk4.equals("-"))) {

gettxtDateOpened().requestFocus();
throw new template.model.ValExc("Date Opened format must be yyyy-mm-dd");
}
if(!(chk7.equals("-"))) {

gettxtDateOpened().requestFocus();
throw new template.model.ValExc("Date Opened format must be yyyy-mm-dd");
}

//close date can't be before open date
java.sql.Date closeDate = java.sql.Date.valueOf(gettxtDateClosed().getText());
java.sql.Date openDate = java.sql.Date.valueOf(gettxtDateOpened().getText());
if (closeDate.before(openDate)) 
{
gettxtDateClosed().requestFocus();
throw new template.model.ValExc("The closing date cannot be set before the opening date.");
}//end if

//validation for notes
if(getJTextAreaNotes().getText().trim().length() > 256)
{
getJTextAreaNotes().requestFocus(); 
throw new template.model.ValExc("Notes must be no more than 256 characters long.");
}

//check for numbers
checkForNumber();

return;
}

     




Essentials of eBusiness / Java / Visual Basic / DB2 / Peer Evaluations / Biography