regarding date field validation

Ads
 

regarding date field validation

View Answers

November 7, 2008 at 1:06 PM

Hi friend,

<html>
<title>date validation in javascript</title>
<head>

<script language = "Javascript">
var date= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
var i;
for (i = 0; i < s.length; i++){
// Check that current character is number.
var c = s.charAt(i);
if (((c < "0") || (c > "9"))) return false;
}
// All characters are numbers.
return true;
}

function stripCharsInBag(s, bag){
var i;
var returnString = "";
// Search through string's characters one by one.
for (i = 0; i < s.length; i++){
var c = s.charAt(i);
if(bag.indexOf(c) == -1) returnString += c;
}
return returnString;
}

function daysInFebruary(year){
// February has 29 days in any year evenly divisible by four,
// EXCEPT for centurial years which are not also divisible by 400.
return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
for (var i = 1; i <= n; i++) {
this[i] = 31
if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
if (i==2) {this[i] = 29}
}
return this
}
function isDate(dtStr){
var daysInMonth = DaysArray(12)
var pos1=dtStr.indexOf(date)
var pos2=dtStr.indexOf(date,pos1+1)
var strMonth=dtStr.substring(0,pos1)
var strDay=dtStr.substring(pos1+1,pos2)
var strYear=dtStr.substring(pos2+1)
strYr=strYear
if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
for (var i = 1; i <= 3; i++) {
if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
}
month=parseInt(strMonth)
day=parseInt(strDay)
year=parseInt(strYr)
if (pos1==-1 || pos2==-1){
alert("The date format should be : mm/dd/yyyy")
return false
}
if (strMonth.length<1 || month<1 || month>12){
alert("Please enter a valid month")
return false
}
if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
alert("Please enter a valid day")
return false
}
if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
return false
}
if (dtStr.indexOf(date,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, date))==false){
alert("Please enter a valid date")
return false
}
return true
}

function ValidateForm(){
var dt=document.frmSample.txtDate
if (isDate(dt.value)==false){
dt.focus()
return false
}
return true
}

</script>
</head>

November 7, 2008 at 1:07 PM

<body>
<br></br>
<center>
<table border="1" width="50%" bgcolor="pink">
<tr>
<td width="100%">
<form name="frmSample" method="post" action="" onSubmit="return ValidateForm()">
<h2><font color="red">Data Validation</font></h2>
<table border="1" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td> <p>Enter a Date <font color="#CC0000"><b>(mm/dd/yyyy)</b></font>
: </td>
<td>
<input type="text" name="txtDate" maxlength="10" size="20">
</p></td>
</tr>
<tr>
<td><center>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="reset" value="Reset">
</center>
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</center>
</body>
</html>

---------------------------------

Amardeep

Thanks

Ads









Related Tutorials/Questions & Answers:
Date Validation
Date Validation  Hi, I need Date Validation using java in spring framework. Please Anyone help me... Thanks in advance
date validation
date validation  sir, pls provide date validation code in javascript..we want to include it into our master form..   Please visit the following link: http://www.roseindia.net/mysql/datevalidation.shtml
Advertisements
Date validation
Date validation  How to validate date in jsp? That is i have a textbox which will have the value from the date picker. How to validate the date that should be entered in a specified format?   Hi Friend, Please visit
regarding out put display in html text field
regarding out put display in html text field  how to dispaly the output coming from servlet in a particular text field of html
how to perform a validation for the field
how to perform a validation for the field  hi, function checkForm() { var getBank=document.form.bname.value; var alphaExp = /^[\,\ \-0-9a-zA-Z]+$/; var message=""; if(document.form.no.value
how to perform a validation for the field
how to perform a validation for the field  hi, function checkForm() { var getBank=document.form.bname.value; var alphaExp = /^[\,\ \-0-9a-zA-Z]+$/; var message=""; if(document.form.no.value
javascript date validation using regex
javascript date validation using regex  Hi, I want to validate the date using javascript. Please help.   <html> <head> <title>Validating Date format</title> <script type="text/javascript">
Mysql date field
Mysql date field       Mysql date field help you to create the data field and their respective data... date field in Mysql. The create table statement is used to create a table
How to Define Text Field Validation iPhone
How to Define Text Field Validation iPhone  Hi, Can anybody explain me how to define textfield validation iPhone program. please phone online help reference or example. Thanks
jquery start date end date validation
jquery start date end date validation  Using JQuery how can i validate the end date and start date? for example if the start date is grater than the end date?   jQuery.validator.addMethod("greaterThan", function(value
File format validation and text field validation in java swings
File format validation and text field validation in java swings   i am reading a jpg image file throuh jfilechooser in java swings,when we press browse button for selecting file it will display filechooser in this process when
validation
validation  validation
i want validation for one text field and text area,email,combobox, in that email will validte by useing regular expressions?
i want validation for one text field and text area,email,combobox, in that email will validte by useing regular expressions?  i want validation for one text field and text area,email,combobox, in that email will validte by useing
validation
validation  we are doing payroll system project in java.So pls provide the complete validation code for employee master form
validation
validation  please help me to check validation for <form>...;/tr> <tr> <td> <label>Date :</label> </td>...; <label> Date Picker</label>
validation.....
validation.....  hi.......... thanks for ur reply for validation code. but i want a very simple code in java swings where user is allowed to enter only numerical values in textbox , if he enters string values then it should
date
date   how to insert date in database? i need coding
Mysql Date Only
a current date as field name 'DateOnly'. date(now ( ) ) : The date(now ( )) return you  a current date value as Date Only field name.  Query... Mysql Date Only      
validation
Validation method valid the in put. otherwise call the javascript onsubmit to check... code.Ok if you use struts then you can use Validation method valid the in put
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
Validation
; <b>Date of birth</b> :<input type="date" name="dob">
Validation
; <b>Date of birth</b> :<input type="date" name="dob">
Validation
); JLabel label=new JLabel("JTable validation Example",JLabel.CENTER); JPanel panel=new JPanel(); panel.add(scroll); JFrame frame=new JFrame("JTable validation
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
Mysql Date Minimum
; Mysql Date Minimum return the minimum record value of a date field from... records value of a date field from a table 'employees'. To begin with it, we... field Name 'date' as Min Date from table employee1. SYNTAX:-Select Min(DateFieldName
regarding rev
regarding rev  write a prog. in java that display all the arguments passed at the command line in reverse order

Ads