Current location: Hot Scripts Forums » General Web Coding » JavaScript » javascript code to restrict user to enter date in specific format


javascript code to restrict user to enter date in specific format

Reply
  #1 (permalink)  
Old 05-20-11, 10:19 AM
faisals999 faisals999 is offline
Newbie Coder
 
Join Date: May 2011
Location: Sargodha, Pakistan
Posts: 27
Thanks: 4
Thanked 0 Times in 0 Posts
javascript code to restrict user to enter date in specific format

hey everyone.............
I want a java script code to restrict user to enter date in specific format.. for example I want user to enter it in this format yyyy/mm/dd
and if user try to enter date in anyother format then this script prohibit user to do that. and ask him to enter date in right format
Reply With Quote
  #2 (permalink)  
Old 05-25-11, 12:22 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
This should get you started.
HTML Code:
<html>
<head>
<SCRIPT LANGUAGE="JavaScript"><!--
function doMatch()
{
 var re = new RegExp(document.demoMatch.regex.value);
 if(document.demoMatch.subject.value.match(re))
 {
  var dateParts = document.demoMatch.subject.value.split("/");
  if(dateParts[1] > 0 && dateParts[1] < 13 && dateParts[2] > 0 && dateParts[2] < 32)
  {
   document.getElementById("results").innerHTML = "Successful match";
   //return true;
   }
  else
  {
   document.getElementById("results").innerHTML = "No match";
   }
  }
 else
 {
  document.getElementById("results").innerHTML = "No match";
  }
 return false;
 }
// -->
</SCRIPT>
</head>
<body>
<FORM ID="demoMatch" NAME="demoMatch" METHOD=POST ACTION="#" onsubmit="return doMatch()">
<INPUT TYPE=hidden NAME="regex" VALUE="\d{4}/\d{2}/\d{2}" SIZE=50>
<P>Date(yyyy/mm/dd): <INPUT TYPE=TEXT NAME="subject"
   VALUE="" SIZE=50></P>
<P><INPUT TYPE=SUBMIT VALUE="Test Match"></P>
<div id="results"></div>
</FORM>
</body>
</html>
__________________
Jerry Broughton
Reply With Quote
Reply

Bookmarks

Tags
date format, javascript


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need a php script that directs user to specific directory mtanner Script Requests 5 06-11-04 02:02 PM
Need Epinions-lite system in PHP & MYSQL wali001 Job Offers & Assistance 4 01-12-04 06:02 AM
Need to send newsletter/letter during specific date casbboy PHP 7 01-04-04 06:55 PM
date format tcooper PHP 6 12-18-03 12:24 PM
Need a simple user code! dave111 Script Requests 1 09-07-03 09:48 AM


All times are GMT -5. The time now is 02:04 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.