Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
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