What is
Data Validation?
Data
Validation is the process of ensuring that a program operates on clean, correct
and useful data. It uses routines, often called "validation rules" or
"check routines", that check for correctness, meaningfulness, and
security of data that are input to the system.
Example - A name text field should not allow user to enter numbers or characters such as '+','=',''&', etc. Mobile numbers should only include digits and '+' sign.
Example - A name text field should not allow user to enter numbers or characters such as '+','=',''&', etc. Mobile numbers should only include digits and '+' sign.
Some Useful
Data Validation Code
- Allowing only Numeric values in a text field
For this purpose we should use
KeyTyped event handler of textField. Every KeyTyped event handler receives a
KeyEvent object by default.
Char kc=evt.getKeyChar();
If(!(kc>=’0’ &&
kc<=’9’))
Evt.consume();
private void jTextField4KeyTyped(java.awt.event.KeyEvent
evt) {
char kc =
evt.getKeyChar();
if (!(((kc
>= '0') && (kc <= '9')) || kc == KeyEvent.VK_BACKSPACE)) {
evt.consume();
}
- Checking for 'No Input'
Users often miss out to enter data in some fields or sometimes they assume a necessary field to be an optional one. So, a program must check if the user has entered the required data or not. If the user has not provided the required data the program must not run further.
Checking for empty textfields - You can check if the textfield is empty or not by the following coding:
if (jTextField1.getText().isEmpty() || jTextField2.getText().isEmpty())
JOptionPane.showMessageDialog(null, "One of the required field is empty!", "Error", JOptionPane.ERROR_MESSAGE);
or you can use the following method:
if (jTextField1.getText().length()==0 ||
jTextField2.getText().length()==0)
JOptionPane.showMessageDialog(null, "One of the required field is
empty!", "Error", JOptionPane.ERROR_MESSAGE);
Here, jTextField1 and jTextField2 are
the fields which cannot be left empty. You can also notify user about the field
that has been left empty by using if..else construct. The same coding, will
work for jTextArea and jPasswordField.
3. Checking for 'No selection from list'
Another run-time error occurs if the user does not select any value from the list component. User must be notified for the same.
if (jList1.getSelectedIndex()==-1)
JOptionPane.showMessageDialog(null, "Please select a value from list");
This will show a message to user for a null value from list
i.e no value selected by user.
4. Checking for 'No selection from Radio buttons'
Checking if user has not selected any choice from a group of radio buttons.
Remember, always put radio buttons in a button group, otherwise all radio buttons could be selected at a particular time.
4. Checking for 'No selection from Radio buttons'
Checking if user has not selected any choice from a group of radio buttons.
Remember, always put radio buttons in a button group, otherwise all radio buttons could be selected at a particular time.
if (jRadioButton1.isSelected()==false &&
jRadioButton2.isSelected()==false)
JOptionPane.showMessageDialog(null, "Please Select your Gender!");
JOptionPane.showMessageDialog(null, "Please Select your Gender!");
or you can try the following code:
if (jRadioButton1.isSelected())
gender="Male";
//Instead do your functioning!
else if (jRadioButton2.isSelected())
gender="Female";
//Instead do your functioning!
else
JOptionPane.showMessageDialog(null, "Please Select your Gender!");
You can use the same piece of code for checkbox.
5. Prohibiting characters in numbers field
This code will help you to prohibit users from entering characters other than digits in numbers field like Moblie Number.
Write the following code on the KeyTyped event of a text field.
5. Prohibiting characters in numbers field
This code will help you to prohibit users from entering characters other than digits in numbers field like Moblie Number.
Write the following code on the KeyTyped event of a text field.
char k=evt.getKeyChar();
if (!(k>='0' && k<='9'))
evt.consume();
if (!(k>='0' && k<='9'))
evt.consume();
Now, if a user types characters other than digits, the event
will be consumed.
The below coding will prevent user from typing digits in a field placed for characters(eg- Name field)
The below coding will prevent user from typing digits in a field placed for characters(eg- Name field)
char k=evt.getKeyChar();
if (k>='0' && k<='9')
evt.consume();
if (k>='0' && k<='9')
evt.consume();
6. Prohibiting multiple selection in a list
Set the selectionMode property of list to Single_Selection by the following code to prevent user from selecting multiple items from list. Default isMultiple_Interval_Selection.
Set the selectionMode property of list to Single_Selection by the following code to prevent user from selecting multiple items from list. Default isMultiple_Interval_Selection.
jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
Also, each GUI form must have a Exit and Clear button.
Exit Button -
System.exit(0);
System.exit(0);
Clear Button -
Clearing TextFields
jTextField1.setText("");
jTextField2.setText("");
jTextField2.setText("");
Clearing selection from list
jList1.clearSelection();
Clearing selection from Combo Box
jComboBox1.setSelectedIndex(-1);
Clearing selection from Radio Buttons
buttonGroup1.clearSelection();
Clearing the contents of a table
DefaultTableModel model=(DefaultTableModel)
jTable1.getModel();
int rows=model.getRowCount();
if (rows>0){
for(int j=0; j<rows; j++){
model.removeRow(0);
}
}
int rows=model.getRowCount();
if (rows>0){
for(int j=0; j<rows; j++){
model.removeRow(0);
}
}
I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post.
ReplyDeleteDatavalidation
Haryana Deled
ReplyDeleteJBT admission
ded haryana admission 2022
Deled Haryana
Haryana JBT admission 2022