Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: date validation

  1. #1
    New Coder
    Join Date
    Jan 2004
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    date validation

    hi

    i want to validate a date in the format mm/dd/yyyy.i want users to enter 2 digit month and 2 digit date and 4 digit year.

    i am doing some validation the date should be 1st or 15 th of month it's working fine.i want to alert if they don't enter in this format mm/dd/yyyy.i want to alert a separate message box if date pattern not matches.

    my code is

    var xVal=window.document.frmNew.txtDate.value;
    var xDate = new Date(xVal);
    var today = new Date();
    if (xDate > today && (xDate.getDate()==15 || xDate.getDate()==01))
    {
    return true;
    }
    alert("The effective date should be the 1st or 15th of the month and be greater than current date.");
    window.document.frmNew.txtDate.focus();
    return false;
    }

  • #2
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    	<head>
    		<title>47301</title>
    	</head>
    	<body>
    		<form onsubmit="return validate()">
    			<fieldset>
    				<label for="year">Year</label>
    				<input id="year" type="text" size="4" maxlength="4">
    
    				<label for="month">Month</label>
    				<select id="month">
    					<option value="00">January</option>
    					<option value="01">February</option>
    					<option value="02">March</option>
    					<option value="03">April</option>
    					<option value="04">May</option>
    					<option value="05">June</option>
    					<option value="06">July</option>
    					<option value="07">August</option>
    					<option value="08">September</option>
    					<option value="09">October</option>
    					<option value="10">November</option>
    					<option value="11">December</option>
    				</select>
    
    				<label for="date">Date</label>
    				<select id="date">
    					<option value="01">01</option>
    					<option value="15">15</option>
    				</select>
    
    				<input type="submit">
    			</fieldset>
    		</form>
    	</body>
    	<script type="text/javascript">
    		function validate()
    		{
    			var now = new Date();
    			var sel = new Date(document.getElementById("year").value, document.getElementById("month").value, document.getElementById("date").value);
    
    			if(sel < now)
    			{
    				return false;
    			}
    
    			return true;
    		}
    	</script>
    </html>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •