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(dat e)) {obj.value=obj.value.repla ce(/[^\d/] /g,'');obj .value=obj .value.rep lace(/\/{2 }/g,'/'); return }
if (/^\d{2}$/.test(date)){obj .value=obj .value+'/' ; return }
if (/^\d{2}\/\d{2}$/.test(dat e)){obj.va lue=obj.va lue+'/'; return }
if (!/^\d{1,2}\/\d{1,2}\/\d{4 }$/.test(d ate)) return
test1=(/^\d{1,2}\/?\d{1,2} \/\d{4}$/. test(date) )
date=date.split('/')
d=new Date(date[2],date[1]-1,dat e[0])
test2=(1*date[0]==d.getDat e() && 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
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(dat
if (/^\d{2}$/.test(date)){obj
if (/^\d{2}\/\d{2}$/.test(dat
if (!/^\d{1,2}\/\d{1,2}\/\d{4
test1=(/^\d{1,2}\/?\d{1,2}
date=date.split('/')
d=new Date(date[2],date[1]-1,dat
test2=(1*date[0]==d.getDat
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
How does a valid date look like for you?
ASKER
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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.showDat e.value=ne w Date(this.value)">
<br>
<input type="text" size="50" name="showDate">
</form>
<form>
<input type="text" size="50" onKeyUp="this.form.showDat
<br>
<input type="text" size="50" name="showDate">
</form>
ASKER
Thanks everybody for nice contributions. Finally, I had to rewrite my script based mostly on pravinasar solutions.
All the best
Andy
All the best
Andy
ASKER
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