Validate JTexField with date on Text Changed event

Ads

 
 

Share on Google+Share on Google+

sushant
Validate JTexField with date on Text Changed event
1 Answer(s)      7 years ago
Posted in : Java Beginners
Hello Sir I want to only Validate date in MM/DD/YYYY Format on TextChanged Event,date will automatically set through MM with two digits,then cursor will automatically set to next DD of Two digits and same as year of four digits within one JTextField and also have onTextChanged Validation ,plz Help me sir
Ads
View Answers

April 10, 2010 at 11:53 AM


Hi Friend,

Try the following code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.*;
class Validations extends JFrame{
JLabel l1,l2,l3,l;
JLabel lab1,lab2,lab3;
JTextField t1,t2,t3;
JPanel p;

Validations(){
l1=new JLabel("Enter Date: (MM/dd/yyyy)");
t1=new JTextField(20);

lab1=new JLabel();

p=new JPanel(new GridLayout(2,2));
p.add(l1);
p.add(t1);
p.add(lab1);
add(p);
setVisible(true);
pack();
lab1.setVisible(false);
t1.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String date=t1.getText();
if(isValidDate(date)){
lab1.setVisible(false);
}
else{
lab1.setText("* Invalid date");
lab1.setForeground(Color.red);
lab1.setVisible(true);
}
}
});

}
public boolean isValidDate(String inDate) {

if (inDate == null)
return false;
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
dateFormat.setLenient(false);
try {
dateFormat.parse(inDate.trim());
}
catch (ParseException pe) {
return false;
}
return true;
}
public static void main(String[]args){
Validations v=new Validations();
}
}

Thanks

Ads









Related Tutorials/Questions & Answers:
Validate JTexField with date on Text Changed event - Java Beginners
Validate JTexField with date on Text Changed event  Hello Sir I want to only Validate date in MM/DD/YYYY Format on TextChanged Event,date...; Validations(){ l1=new JLabel("Enter Date: (MM/dd/yyyy)"); t1=new JTextField(20
JavaScript Validate UK Date
JavaScript Validate UK Date  How to Validate UK Date in JavaScript... if (!validformat.test(input.value)) alert('Invalid Date Format. Please correct.') else{ //Detailed check for valid date ranges var
Advertisements
validate text field iPhone
validate text field iPhone  i got two different UITextfield in my iPhone application for user login. Just wondering how to validate
JQuery date change event
JQuery date change event  JQuery date change event I have been... date and end date for any event. I have already writen a function that allows... = 6; then my end date must be changed to 7 Jan 2013
Text event in jsp
Text event in jsp  Hi I am doing project in jsp with mysql...="text/javascript"> function showData(value){ xmlHttp=GetXmlHttpObject() var url... type="text" name="name" id="name" onkeyup="showData(this.value);"><
Text box control--keypress event
Text box control--keypress event  In my form have a text box...My requirement is... when is type characters in that box... before typing the 3rd character the space should come automatically or programmatically...the sturucture
Validate
Validate   hi all..im a beginner java and i need some help from u guys.. Its about validate. My HR request BF = 'brought forward', leave cannot apply date greater than 31/03 of particular year. So, if the user apply the date
How to Validate dynamically created text box in HTML using Javascript
How to Validate dynamically created text box in HTML using Javascript  Here is the HTML code to create text boxes dynamically. It works properly.... And also how to validate the text boxes created dynamically. <html>
Javascript get Date And Selected Option Text
Javascript get Date And Selected Option Text In this tutorial we will learn... explains you that how to display the date and the selected option text in Javascript... the date and the selected option text into HTML. Example Here I am giving a simple
javascript cal code for selecting date in text field - JSP-Servlet
javascript cal code for selecting date in text field  HI I want javascript calendar code to select date in text field in jsp.pls send me?  Hi Friend, Please visit the following links: http://www.roseindia.net
Display current date in text box in JSP using javascript
Display current date in text box in JSP using javascript In this section you will learn how to display current date in text box using JavaScript.For...; Today's Date is: <input type="text" id="datetext"> <
Validate TextArea
Validate TextArea       In this section, you will learn how to validate your text area in struts 2. A text area contains 1 to 250 characters. It cann't support
JavaScript regex validate validate Zip.
JavaScript regex validate validate Zip.  JavaScript regex validate - how to validate Zip?   <html> <head> <title>Zip Code validation using regex</title> <script type="text/javascript">
Check Date in PHP
Check Date in PHP The Checkdate() The PHP Checkdate function is used for validating the system date. It validate the Gregorian date. It returns 'True' if finds the specified date valid, otherwise it returns false. Note
long validate
long validate  How to validate long type
date
date   how to insert date in database? i need coding
date
database table name birthday (DOB date); dob with DATE data type in database while..., ParseException { response.setContentType("text/html;charset=UTF-8...=d.getTime(); java.sql.Date date = new java.sql.Date(t); try
date
date  can u tell me how to calculate difference between a user provided date and the system date in java
date
date  can u tell me how to calculate difference between a user provided date and the system date in java
date
date  i want difference between the date entered in a jtextfield and the system date in java
date
date  how to insert date using html through the combobox
date
date  how to insert date using html through the combobox
date
date  how to insert date using html through the combobox
date
date  how to insert date using html through the combobox
DATE
DATE  I have the following as my known parameter Effective Date... of calcultion starts My question is how to find the date of Thursday This cycle... the date of all the thursdays of the given year. import java.util.*; import
javascript regex validate url
regex</title> <script type="text/javascript"> function validate...javascript regex validate url  How to validate URL in regex...;Validating Url..</h2> Url : <input type="text" name="url" id="url" />
JavaScript regex validate Telephone no.
JavaScript regex validate Telephone no.  JavaScript regex validate... validation using regex</title> <script type="text/javascript"> function validate() { var phone = document.getElementById("phone").value
javascript regex validate currency
javascript regex validate currency  How to validate currency... using regex</title> <script type="text/javascript"> function validate() { var currency = document.getElementById("currency").value
JavaScript regex validate Mobile no.
JavaScript regex validate Mobile no.  JavaScript regex validate... validation using regex</title> <script type="text/javascript"> function validate() { var mobile = document.getElementById("mobile").value
JavaScript regex validate Character
JavaScript regex validate Character  JavaScript regex validate... using regex</title> <script type="text/javascript"> function validate() { var name = document.getElementById("name").value; var

Ads

 
Advertisement null

Ads