Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
<script type="text/javascript">
    function isValidDate(sText) {
        var reDate = /(?:0[1-9]|[12][0-9]|3[01])\/(?:0[1-9]|1[0-2])\/(?:19|20\d{2})/;
        return reDate.test(sText);
    }
    function validate() {
        var oInput1 = document.getElementById("txt1");
        if (isValidDate(oInput1.value)) {
            alert("Valid");
        } else {
            alert("Invalid!");
        }

    }
</script>
Posted
Comments
rahulkichur 14-Nov-14 3:09am    
What is requirement whether you want your input control to accept the given format or need to convert to that format in javascript
M.Awais24 14-Nov-14 3:13am    
i want input control to accept dd/mm/yy instead of dd/mm/yyyy

You should change your reg expression like bellow:
C#
var reDate = new RegExp("^(?:0[1-9]|[12][0-9]|3[01])\/(?:0[1-9]|1[0-2])\/\d{2}$");
 
Share this answer
 
Comments
M.Awais24 14-Nov-14 3:26am    
Thank you for your response,, This code is not working even know this code no accepting any year format i mean not accept y-yy-yyy-yyyy
Raul Iloc 14-Nov-14 4:24am    
So, my suggested change it will work for your or not?

PS: I just tested in the next site and it works fine! http://www.regexplanet.com/advanced/java/index.html
Here is a code snippet i have done in html in which i have designed a date picker which accepts the date in the format dd/mm/yy. Hopes it will help you

XML
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

  <title>jQuery UI Datepicker - Default functionality</title>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css"/>
  <script type="text/javascript"src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css"/>
  <script type="text/javascript">
      $(function () {
          $("#datepicker").datepicker({ dateFormat: 'dd/mm/y' });
      });
  </script>
</head>
<body>
 <form id="form1" runat="server">
<p>Date:<input ID="datepicker" runat="server"/></p>
 </form>

</body>

</html>
 
Share this answer
 
Comments
M.Awais24 14-Nov-14 4:01am    
Sorry dear you are not getting my point, i just want in input format, user have to input date manually. I don't want to allow the user choose from calender. Thanks.
Here is a code snippet where mask is applied to textbox so that it will accept data in dd/mm/yy format.

for that you must download the "jquery.inputmask.bundle.min.js" and replace the path in the file.

Hopes it works for you





<title>jQuery UI Datepicker - Default functionality



$(document).ready(function () {
$(":input").inputmask();
});









 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900