Javascript form help needed
Started by
ScHwErV
, Nov 14 2007 02:26 PM
#16
Posted 26 November 2007 - 04:33 PM
#17
Posted 26 November 2007 - 08:33 PM
What I was thinking of is far more simple than what you have. I haven't had time to actually write anything yet, just jotting some things down. The script that I have working right now.
function formatDate (el){ var date = new String(el.value); var regX = new RegExp("\\d{3,}", "g"); var matches = date.match(regX); var fDate = ""; for(var i = 0; i < matches.length; i++) { fDate = fDate + matches[i]; } if (fDate.length = 8) { date = new String(fDate); el.value = date.slice(0,2) + '/' + date.slice(2,4) + '.' + date.slice(4,8); } else{}}What I am thinking of doing is just adding to your if statement.
if (fDate.length < 8)Then popup an alert. "Please enter the numbers in the proper date format MM/DD/YYYY"
#18
Posted 27 November 2007 - 08:26 AM
Yeah I figured it might be more than what you were looking for. Yours looks pretty good to me. What you could do is put the alert in the else clause:
if (fDate.length = 8) { date = new String(fDate); el.value = date.slice(0,2) + '/' + date.slice(2,4) + '.' + date.slice(4,8); } else { alert("Please enter a date in the proper date format: MM/DD/YYYY"); }You still have a residual '.' in the value too.
#19
Posted 27 November 2007 - 09:23 AM
I actually tried that yesterday, for some reason, in the else clause, it doesn't work.
I found that rogue '.' too.
I found that rogue '.' too.
#20
Posted 27 November 2007 - 11:33 AM
The overly simple, final product.
<!-- function formatDate (el) { var date = new String(el.value); var regX = new RegExp("\\d{3,}", "g"); var matches = date.match(regX); var fDate = ""; for(var i = 0; i < matches.length; i++) { fDate = fDate + matches[i]; } if (fDate.length < 8) { alert("Please enter a date in the proper date format: MM/DD/YYYY"); } else if (fDate.length = 8) { date = new String(fDate); el.value = date.slice(0,2) + '/' + date.slice(2,4) + '/' + date.slice(4,8); } else {} }//-->
#21
Posted 27 November 2007 - 11:44 AM
Nice work!
#22
Posted 27 November 2007 - 02:10 PM
You did most of the work, I just modified. Many thanks.
#23
Posted 27 November 2007 - 02:21 PM
It was a pleasure, no problem!
Similar Topics
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users