Introduction We should check whether there is a non-numeric character. If so we should display a message not to use alphabets and other characters.
Even if the user enters a numbers, We will have to split the Day, Month, Year using some conditions like Numbers till first "/" or "-" is Month After Days numbers till second "/" or "-" is Day Next part is Year
We must still take out the Day, Month, Year parts and Validation of Day: 31 for Jan, Mar, etc., 30 for Apr, Jun, etc 28 for Feb in ordinary years 29 for Feb in Leap years.
Months: Entered should be less 12 Years: Must be with in acceptable limits say from 1990 - 2010 or whatever it is.
Approach Towards Date Keeping the above things in mind, Here i go with my idea. The following is the approach to solve the Date. Instead of placing a Textbox control, Place 3 Select Combobox controls, For Month, Day, Year Allow users to select Date what they want, Number validation will be void. Rest of the validations can be done with JavaScript.
Here we have a dateField
2. I am using asp code. Kindly convert to your language
Here is the code
Page1.asp
// java script begins var error=0 var months=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") function validate() { var dayList=document.getElementById("dayField") var purchaseDay=parseInt(dayList.options[dayList.selectedIndex].text)
var monthList=document.getElementById("monthField") var purchaseMonth=monthList.options[monthList.selectedIndex].text
var yearList=document.getElementById("yearField") var purchaseYear=parseInt(yearList.options[yearList.selectedIndex].text)
var pDate=validateDate(purchaseDay,purchaseMonth,purchaseYear)
if(pDate=="") return false
var dateVar=new Date(pDate) if(error==0) { document.form1.dateField.value=pDate return true } else { error=0 return false } }
function validateDate(day,month,year) {
var i
//getting the month value in terms of JavaScript to verify for (i in months) if(months[i]==month) break
if(i==3||i==5||i==8||i==10) //for apr jun sep nov { if(day>30) { alert("Invalid Date: Please Select Correct”) error=1 } else { i++ var newdate=i+"/"+day+"/"+year return(newdate) } }
else if(i==1) // for february month { var febDays if(((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0)))) febDays=29 else febDays=28 if(day>febDays) { alert("Invalid Date: Please Select Correct ") error=1 } else { i++ var newdate=i+"/"+day+"/"+year return(newdate) } } else { i++ var newdate=i+"/"+day+"/"+year return(newdate) } return "" } //Java script ends here
form name: form1 onSubmit validate() acion=somePage.asp
comboxboxes :
"monthField" //Jan, Feb, Mar, etc "dayField"" //0-31 "yearField" from //2000-2020 what ever u want
hidden fields :
dateField
this dateField is passed to next page and u can insert in a database or do what ever
Summary
You can efficiently deal Date with this approach.
Happy Coding!!!
With Regards,
Tamil
|
No responses found. Be the first to respond and make money from revenue sharing program.
|