Results 1 to 2 of 2
- 12-15-2010, 01:49 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 22
- Rep Power
- 0
Error Checking not working correctly
private void cmdSaveActionPerformed(java.awt.event.ActionEvent evt) {
//Set Names to Proper Case
txtFname.setText(pc.toProperCase(txtFname.getText( )));
txtMname.setText(pc.toProperCase(txtMname.getText( )));
txtLname.setText(pc.toProperCase(txtLname.getText( )));
txtAdd1.setText(pc.toProperCase(txtAdd1.getText()) );
txtAdd2.setText(pc.toProperCase(txtAdd2.getText()) );
//Validate Fields
validateNRN(txtNRN.getText());
verifyNRNuniqueness(txtNRN.getText().replaceAll("-", ""));
validateNIS(txtNIS.getText());
validateFname(txtFname.getText());
validateMname(txtMname.getText());
validateLname(txtLname.getText());
validateAdd1(txtAdd1.getText());
validateAdd2(txtAdd2.getText());
validateEmail(txtEmail.getText());
verifyEmailAdd(txtEmail.getText());
validatePnum(txtHnumber.getText());
validatePnum(txtCnumber.getText());
validateHdate(txtHdate.getText());
//Save Data////////////////////////////////////////////////////////////
if (validateNRN(txtNRN.getText().replaceAll("-", "")) == true){
}else if (validateNIS(txtNIS.getText()) == true){
}else if (validateFname(txtFname.getText()) == true){
}else if (validateMname(txtMname.getText()) == true){
}else if (validateLname(txtLname.getText()) == true){
}else if (validateAdd1(txtAdd1.getText()) == true){
}else if (validateAdd2(txtAdd2.getText()) == true){
}else if (validateEmail(txtEmail.getText()) == true){
}else if (verifyEmailAdd(txtEmail.getText()) == true){
}else if (validatePnum(txtHnumber.getText()) == true){
}else if (validatePnum(txtCnumber.getText()) == true){
}else if (validateHdate(txtHdate.getText()) == true){
try {
StringBuffer SQLQuery = new StringBuffer();
SQLQuery.append("INSERT INTO EmployeeDetails (NRN,NIS,FirstName,MiddleName,LastName,MaritalStat us,Address1,Address2,Parish,Email,HomeNumber,CellN umber,HiredDate,JobTitle) VALUES (");
SQLQuery.append(txtNRN.getText().replaceAll("-", ""));
SQLQuery.append(",");
SQLQuery.append(txtNIS.getText());
SQLQuery.append(",'");
SQLQuery.append(txtFname.getText().toString());
SQLQuery.append("','");
SQLQuery.append(txtMname.getText().toString());
SQLQuery.append("','");
SQLQuery.append(txtLname.getText().toString());
SQLQuery.append("','");
SQLQuery.append(cboMaritalStatusLnk.getSelectedIte m().toString());
SQLQuery.append("','");
SQLQuery.append(txtAdd1.getText().toString());
SQLQuery.append("','");
SQLQuery.append(txtAdd2.getText().toString());
SQLQuery.append("','");
SQLQuery.append(cboParishLnk.getSelectedItem().toS tring());
SQLQuery.append("','");
SQLQuery.append(txtEmail.getText().toString());
SQLQuery.append("',");
SQLQuery.append(txtHnumber.getText().replaceAll("-", ""));
SQLQuery.append(",");
SQLQuery.append(txtCnumber.getText().replaceAll("-", ""));
SQLQuery.append(",'");
SQLQuery.append(txtHdate.getText().toString());
SQLQuery.append("','");
SQLQuery.append(cboJtitleLnk.getSelectedItem().toS tring());
SQLQuery.append("');");
JOptionPane.showMessageDialog(opnErrorMessage,"Rec ord was Saved!");
//Transmit data to database
System.out.println(SQLQuery);
dataconnect.querySQLINSERT(SQLQuery.toString());
}catch(SQLException sqle){
this.dispose();
}
}
else{
JOptionPane.showMessageDialog(opnErrorMessage,errM sg);
errMsg.delete(0, errMsg.length());
}
//New Instances and variable declarations
private cnConnection dataconnect = new cnConnection();
private ProperCase pc = new ProperCase();
StringBuffer errMsg = new StringBuffer();
//Verify Email Address
private boolean verifyEmailMatch() {
if (new String(txtEmail.getText()).equals(new String(txtEmailVer.getText())) == true) {
return true;
} else {
return false;
}
}
//Validate Name
public static boolean validateName(String strName) {
return strName.matches("[a-zA-z]+([ '-][a-zA-Z]+)*");
}
//Validate Address
public static boolean validateAddress(String strName) {
return strName.matches("[A-Za-z0-9# '-\\./\"]+[A-Za-z0-9# '-\\./\"]");
}
//Validate Email Address
public static boolean verifyEmail(String strName) {
return strName.matches("[A-Za-z0-9#@!$_\\.-]+@[A-Za-z0-9]*+.[A-Za-z0-9]*");
}
public boolean validateEmail (String emailFields){
//Validate Email
if (txtEmail.getText().isEmpty() == false & verifyEmail(txtEmail.getText()) == false) {
errMsg.append ("\nV3 - Invalid Email, ");
} return false;
}
public boolean verifyEmailAdd (String emailFields){
if (verifyEmailMatch() == false){
errMsg.append ("\nEU4 The Emails you typed do not match");
txtEmail.setBackground(Color.PINK);
txtEmailVer.setBackground(Color.PINK);
return false;
}
else {
txtEmail.setBackground(Color.WHITE);
txtEmailVer.setBackground(Color.WHITE);
return true;
}
}
public boolean validateFname (String FnameField){
//Validate Names
//First Name______________________________________
if (txtFname.getText().equals("")) {
errMsg.append ("\nV4 - Enter First Name");
txtFname.setBackground(Color.PINK);
return false;
}
else if (txtFname.getText().length() == 1){
JOptionPane.showMessageDialog(opnErrorMessage,"V1 - First Name must be greater than 1 character");
txtFname.setBackground(Color.PINK);
return false;
}
else if (validateName(txtFname.getText()) == false){
errMsg.append("\nV7 - Invalid Character in First Name");
txtFname.setBackground(Color.PINK);
return false;
}
else {
txtFname.setBackground(Color.WHITE);
return true;
}
}
public boolean validateMname (String MnameField){
//Middle Name______________________________________
if (txtMname.getText().length() == 1){
errMsg.append ("\nV1 - Middle Name must be greater than 1 character");
txtMname.setBackground(Color.PINK);
return false;
}
else if (txtMname.getText().isEmpty() == false & validateName(txtMname.getText()) == false){
errMsg.append ("\nV7 - Invalid Character in Middle Name");
txtMname.setBackground(Color.PINK);
return false;
}
else
txtMname.setBackground(Color.WHITE);
return true;
}
public boolean validateLname (String LnameField){
//Last Name________________________________________
if (txtLname.getText().equals("") ) {
errMsg.append ("\nV4 - Enter Last Name");
txtLname.setBackground(Color.PINK);
return false;
}
else if (txtLname.getText().length() == 1){
errMsg.append("\nV1 - Last Name must be greater than 1 character");
txtLname.setBackground(Color.PINK);
return false;
}
else if (validateName(txtLname.getText()) == false){
errMsg.append("\nV7 - Invalid Character in Last Name");
txtLname.setBackground(Color.PINK);
return false;
}
else {
txtLname.setBackground(Color.WHITE);
}return true;
}
public boolean validateNRN (String nrnField){
//Validate NRN
if (txtNRN.getText().equals(" - ")){
errMsg.append ("\nV4 - Enter NRN");
txtNRN.setBackground(Color.PINK);
return false;
}
else if (txtNRN.getText().length() < 10){
errMsg.append ("\nV2 - NRN out of range");
txtNRN.setBackground(Color.PINK);
return false;
}
else if (verifyNRNuniqueness(txtNRN.getText().replaceAll("-", "")) == false ){
errMsg.append ("\nIO4 - NRN already exist");
txtNRN.setBackground(Color.PINK);
return false;
}
else {
txtNRN.setBackground(Color.WHITE);
return true;
}
}
public boolean validateNIS (String nisField){
//Validate NIS
if (txtNIS.getText().equals(" ")){
errMsg.append ("V4 - Enter NIS");
txtNIS.setBackground(Color.PINK);
return false;
}
else if (txtNIS.getText().length() < 6){
errMsg.append("V2 - NIS out of range");
txtNIS.setBackground(Color.PINK);
return false;
}
else if (verifyNISuniqueness(txtNIS.getText().replaceAll("-", "")) == false ){
errMsg.append ("\nIO4 - NIS already exist");
txtNIS.setBackground(Color.PINK);
return false;
}
else {
txtNIS.setBackground(Color.WHITE);
return true;
}
}
public boolean validateAdd1 (String add1Field){
//Validate Addresses
//Address Line 1
if (txtAdd1.getText().equals("")){
errMsg.append ("\nV4 - Enter Address Line 1");
txtAdd1.setBackground(Color.PINK);
return false;
}
else if (validateAddress(txtAdd1.getText()) == false) {
errMsg.append ("\nV7 - Invalid Character in Address Line 1");
txtAdd1.setBackground(Color.PINK);
return false;
}
else {
txtAdd1.setBackground(Color.WHITE);
return true;
}
}
public boolean validateAdd2 (String add2Field){
//Address Line 2
if (txtAdd2.getText().equals("")){
errMsg.append ("\nV4 - Enter Address Line 2");
txtAdd2.setBackground(Color.PINK);
return false;
}
else if (validateAddress(txtAdd2.getText()) == false) {
errMsg.append("\nV7 - Invalid Character in Address Line 2");
txtAdd2.setBackground(Color.PINK);
return false;
}
else {
txtAdd2.setBackground(Color.WHITE);
return true;
}
}
public boolean validatePnum (String hNumField){
//Validate Phone Numbers
if (txtHnumber.getText().equals(" - ") & txtCnumber.getText().equals(" - ")){
errMsg.append ("\nV4 - Enter Contact Number");
txtHnumber.setBackground(Color.PINK);
txtCnumber.setBackground(Color.PINK);
return false;
}
else {
txtHnumber.setBackground(Color.WHITE);
txtCnumber.setBackground(Color.WHITE);
return true;
}
}
public boolean validateHdate (String hDateField){
//Validate Hired Date
if (txtHdate.getText().equals(" / / ")){
errMsg.append ("\nV4 - Enter Hired Date");
txtHdate.setBackground(Color.PINK);
return false;
}
else {
txtHdate.setBackground(Color.WHITE);
return true;
}
}
private boolean verifyNRNuniqueness(String strNRN) {
try {
dataconnect.openSQLSELECT("SELECT COUNT(*) AS Amount FROM EmployeeDetails WHERE NRN = " + strNRN.replaceAll("-", "") + "");
if (dataconnect.rs.next()) {
if (dataconnect.rs.getInt("Amount") == 0) {
dataconnect.closeSQLSELECT();
return true;
}
}
dataconnect.closeSQLSELECT();
} catch (SQLException sqle) {
} catch (NullPointerException npe) {
}
return false;
}
private boolean verifyNISuniqueness(String strNIS) {
try {
dataconnect.openSQLSELECT("SELECT COUNT(*) AS Amount FROM EmployeeDetails WHERE NIS = " + strNIS.replaceAll("-", "") + "");
if (dataconnect.rs.next()) {
if (dataconnect.rs.getInt("Amount") == 0) {
dataconnect.closeSQLSELECT();
return true;
}
}
dataconnect.closeSQLSELECT();
} catch (SQLException sqle) {
} catch (NullPointerException npe) {
}
return false;
}
- 12-15-2010, 01:54 PM #2
Member
- Join Date
- Dec 2010
- Posts
- 22
- Rep Power
- 0
I feel there is something wrong with my if statements but i just can't figure it out.
It should check all fields when the save button is clicked and bring up a single message box stating the errors found. then the SQL statement is telling me that no null values cant be entered. Middle name, email address and either the H number or C number can be empty if one or the other has data.
Similar Threads
-
HTML Links on IzPack HTMLInfoPanel not working correctly
By salman.kagzi in forum New To JavaReplies: 0Last Post: 11-30-2010, 05:07 PM -
Error working with UPS/FedEx Shipping API..
By Kabiraa in forum XMLReplies: 15Last Post: 03-26-2010, 08:12 PM -
My rotate 2d pos method isnt working correctly..
By Addez in forum New To JavaReplies: 5Last Post: 12-01-2009, 09:04 AM -
Gueesing Game Almost done, but not working correctly
By mbnumba6 in forum New To JavaReplies: 5Last Post: 03-18-2009, 03:01 AM -
[SOLVED] \t not working correctly?
By Gakusei in forum New To JavaReplies: 5Last Post: 05-06-2008, 04:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks