Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Javascript form help needed


  • Please log in to reply

#16
Ax238

Ax238

    Tech Staff

  • Technician
  • 1,323 posts
Sounds good, no problem at all, just making sure. I've come up with a modified version of the previous script that can do what you need for the date formatting. All the user needs to do is enter at least a four digit date. I'd like to see what you come up with before I show it to you. Mine may not be as simple as you want though.
  • 0

Advertisements


#17
ScHwErV

ScHwErV

    Member 5k

  • Topic Starter
  • Retired Staff
  • 21,285 posts
  • MVP
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"
  • 0

#18
Ax238

Ax238

    Tech Staff

  • Technician
  • 1,323 posts
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.
  • 0

#19
ScHwErV

ScHwErV

    Member 5k

  • Topic Starter
  • Retired Staff
  • 21,285 posts
  • MVP
I actually tried that yesterday, for some reason, in the else clause, it doesn't work.

I found that rogue '.' too. :)
  • 0

#20
ScHwErV

ScHwErV

    Member 5k

  • Topic Starter
  • Retired Staff
  • 21,285 posts
  • MVP
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		{}	    	}//-->

  • 0

#21
Ax238

Ax238

    Tech Staff

  • Technician
  • 1,323 posts
:) Nice work!
  • 0

#22
ScHwErV

ScHwErV

    Member 5k

  • Topic Starter
  • Retired Staff
  • 21,285 posts
  • MVP
You did most of the work, I just modified. Many thanks.
  • 0

#23
Ax238

Ax238

    Tech Staff

  • Technician
  • 1,323 posts
:) It was a pleasure, no problem!
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP