Input Formatting: Masks, Filters, and Real-Time Validation Techniques
Masks, Filters, and Real-Time Validation Techniques in Java
Introduction
जब हम Java GUI Applications (specially Swing) में data entry forms बनाते हैं, तो हमें यह ensure करना होता है कि user जो input दे रहा है वह सही format में हो। जैसे — Mobile Number हमेशा 10 digits का होना चाहिए, Email valid होना चाहिए, या Date सही format (dd/mm/yyyy) में हो। ऐसे cases में हम Masks, Filters, और Real-Time Validation techniques का use करते हैं।
इन techniques का main purpose है — user input को restrict, validate, और format करना ताकि application में गलत data entry न हो और system अधिक reliable बने।
What is Masking?
Masking एक ऐसी technique है जिससे हम input field में data का fixed format define कर सकते हैं। यानी user को उसी pattern में data डालना होगा जैसा हमने mask में specify किया है। Java में Masking के लिए हम JFormattedTextField और MaskFormatter का use करते हैं।
Example: Masked Input for Phone Number
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
public class MaskExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Mask Example");
frame.setLayout(new FlowLayout());
try {
MaskFormatter mask = new MaskFormatter("##########"); // 10 digits
JFormattedTextField phoneField = new JFormattedTextField(mask);
phoneField.setColumns(10);
frame.add(new JLabel("Mobile Number: "));
frame.add(phoneField);
} catch (Exception e) {
e.printStackTrace();
}
frame.setSize(250,150);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Explanation
- MaskFormatter("##########") का मतलब है कि input केवल numbers (0-9) हो सकते हैं और length fixed 10 होगी।
- JFormattedTextField automatically invalid input reject कर देता है।
Common Mask Patterns
| Symbol | Meaning | Example |
|---|---|---|
| # | Digit (0-9) | ########## → Phone Number |
| A | Letter (A-Z or a-z) | AA#### → ID Format |
| * | Any character | ****-**** |
| U | Uppercase letter | UU#### |
| L | Lowercase letter | LL#### |
What is Filtering?
Filtering का मतलब होता है कि input के characters को control करना — यानी user कौन सा character type कर सकता है और कौन सा नहीं। इसके लिए Java में हम DocumentFilter class का use करते हैं। यह method real-time में हर character को check करती है जब user टाइप करता है।
Example: Allow Only Alphabets in TextField
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
public class FilterExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Filter Example");
JTextField textField = new JTextField(15);
((AbstractDocument) textField.getDocument()).setDocumentFilter(new DocumentFilter() {
public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
if (string.matches("[a-zA-Z]+"))
super.insertString(fb, offset, string, attr);
}
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
if (text.matches("[a-zA-Z]+"))
super.replace(fb, offset, length, text, attrs);
}
});
frame.add(new JLabel("Name: "));
frame.add(textField);
frame.setLayout(new FlowLayout());
frame.setSize(250,120);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Explanation
- DocumentFilter हर बार check करता है कि user क्या टाइप कर रहा है।
- अगर input सिर्फ alphabets हैं तो वह allow होता है, नहीं तो reject।
- यह approach real-time filtering करती है।
Other Filtering Examples
- Only Numbers →
string.matches("[0-9]+") - Only Uppercase →
string.matches("[A-Z]+") - Only Lowercase →
string.matches("[a-z]+") - Alpha-Numeric →
string.matches("[a-zA-Z0-9]+")
What is Real-Time Validation?
Real-time validation का मतलब है input को उसी समय check करना जब user टाइप करता है। यह technique UI level पर होती है और इससे user तुरंत जान सकता है कि input valid है या नहीं। यह system को ज्यादा interactive और user-friendly बनाती है।
Example: Email Validation in Real-Time
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.regex.*;
public class RealTimeValidation {
public static void main(String[] args) {
JFrame frame = new JFrame("Real-Time Validation");
JTextField emailField = new JTextField(20);
JLabel message = new JLabel();
emailField.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
String email = emailField.getText();
Pattern pattern = Pattern.compile("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-z]{2,6}$");
if (pattern.matcher(email).matches()) {
message.setText("✅ Valid Email");
message.setForeground(Color.GREEN);
} else {
message.setText("❌ Invalid Email");
message.setForeground(Color.RED);
}
}
});
frame.setLayout(new FlowLayout());
frame.add(new JLabel("Enter Email: "));
frame.add(emailField);
frame.add(message);
frame.setSize(300,150);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Explanation
- Regex pattern email format verify करता है।
- अगर user valid email डालता है तो message green में show होता है।
- गलत email पर तुरंत error show होता है — यही है real-time validation।
Comparison Between Masking, Filtering and Validation
| Feature | Masking | Filtering | Validation |
|---|---|---|---|
| Purpose | Format fix करना | Character control करना | Input correctness check करना |
| Library/Class | MaskFormatter, JFormattedTextField | DocumentFilter | Regex, Event Listeners |
| Timing | Before typing | During typing | After typing (real-time) |
| Use Case | Phone, Date, PIN | Name, ID Input | Email, Password, Age |
Best Practices
- Always use MaskFormatter for fixed-length data like Date, Phone Number, Zip Code।
- DocumentFilter का use करें जब आपको केवल कुछ specific characters allow करने हों।
- Real-time validation में Regex का use करें ताकि pattern easily verify हो सके।
- Validation के साथ-साथ error message भी दिखाएं ताकि user को तुरंत पता चले क्या गलत है।
- सभी inputs को final submit से पहले server-side validation से भी pass करें।
Exam Notes (Short Summary)
- Masking: Data entry के लिए fixed format set करना। Class –
MaskFormatter. - Filtering: Specific characters को allow/restrict करना। Class –
DocumentFilter. - Real-Time Validation: Input को typing के समय validate करना। Tools –
Regex,KeyListener. - Masking predefined pattern देता है जबकि Filtering user ke typed character को control करता है।
- Real-time validation user experience को better बनाता है।