Link to home
Start Free TrialLog in
Avatar of andy7789
andy7789

asked on

Jscript date-time input mask validation

Hi X-perts,

I need a simple script to validate user's unput of date & time with some mask. It should check input of nymbers only and give a slash prompt, when input is correct.  have found a lot of scripts, but not exactly what I need. This script below check only date but not time:

<script>
function validDate(obj){
 date=obj.value
if (/[^\d/]|(\/\/)/g.test(date))  {obj.value=obj.value.replace(/[^\d/]/g,'');obj.value=obj.value.replace(/\/{2}/g,'/'); return }
if (/^\d{2}$/.test(date)){obj.value=obj.value+'/'; return }
if (/^\d{2}\/\d{2}$/.test(date)){obj.value=obj.value+'/'; return }
if (!/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(date)) return

 test1=(/^\d{1,2}\/?\d{1,2}\/\d{4}$/.test(date))
 date=date.split('/')
 d=new Date(date[2],date[1]-1,date[0])
 test2=(1*date[0]==d.getDate() && 1*date[1]==(d.getMonth()+1) && 1*date[2]==d.getFullYear())
 if (test1 && test2) return true
 alert("Invalid date")
 obj.select();
 obj.focus()
 return false
}
</script>
<body>
<p>Date1: <input type="text" name="startDate" size="9" maxLength="10"
onkeyup="validDate(this)"  onblur="validDate(this)"> </p>
</body>
</html>

What should be fixed to add time input to the same field in the form HH:MM (24h format)? Maybe someone has a better solution...

Thanks

Andy
SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of andy7789
andy7789

ASKER

Thanks mplungjan

It is not exactly what I am thinking of. It has to be just a single typing field, but not a few dropdown menus. Obviously, it is a good idea to validate date and month upon input. The 1st script validates the whole string, not the date-month.

Andy
How does a valid date look like for you?
If we forget about leap years we should have at least

1. date  - 30 or 31 days depending on a given month and 28 days for Feb
2. 1<= month<=12
3. year>=2006  
I mean, how does the string look like that the user MUST enter
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
mplungjan,

Sorry, it seems that my reply to your post is missing;) The input string should look like:

dd/mm/yyyy hh:mm or
dd-mm-yyyy hh:mm

After date or month input there should be an automatic slash prompt (like the mask in the 1st code). The script what I am looking for should not only validate the input, but act as a mask

Best,

Andy
What is "an automatic slash prompt"

Too bad this does not work 100% (due to annoying americanism)

<form>
<input type="text" size="50" onKeyUp="this.form.showDate.value=new Date(this.value)">
<br>
<input type="text" size="50" name="showDate">
</form>
Thanks everybody for nice contributions. Finally, I had to rewrite my script based mostly on pravinasar solutions.
All the best
Andy