• 09-06-2021, 11:49:01
    #1
    İnternette aradıgım bir çok js kütüphanesinde dogum tarihinde standart kontroller sağlamakta. 01.01.1984 şeklinde verilmesini sağlanıyor. kişi isterse 13.13.1831 de yazabiliyor. bu durumu js tarafında nasıl engelliyorsunuz?

    Örnek Kod:
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title></title>
    <link href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css" rel="Stylesheet"
        type="text/css" />
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
    <script language="javascript">
        $(document).ready(function () {
            $("#txtdate").datepicker({
                minDate: 0
            });
        });
    </script>
    </head>
    <body>
    Date :
    <input id="txtdate" type="text">
    </body>
    </html>
  • 09-06-2021, 20:49:29
    #2
    Bu tür durumlarda input'un date özelliğini kullanmanız gerekiyor, spesifik olarak max-min ve default şekilde belirleyebiliyorsunuz.

    <input type="date" id="start" value="2015-02-22"  min="2010-01-01" max="2020-06-09">
    Benim önerim yukarıdaki date özelliğini kullanmanız, fakat text kullanma zorunluluğunuz varsa aşağıdaki kodu deneyebilirsiniz, üstünkörü yazdığım için birkaç hata çıkabilir


       <input type="text" maxlength="10">
        <p style="color: red;" class="hata"> </p>
        
        <script>
        
            let arr = new Array();
                
            let text = document.querySelector("input");
            let hata = document.querySelector(".hata");
            
     
            
            
            
                text.addEventListener('keydown', function() {
              
                if(text.value.length == 2) {
    
                    text.value += '/';
                }
                    
                  if(text.value.length == 5) {
    
                    text.value += '/';
                      
    
                }  
                    
                  
            } )
            
            
             text.addEventListener('change', function() {
              
              arr = text.value.split('/');
                
               if(arr[0] > 31 || arr[1] > 12 || arr[2] > 2022) {
                   hata.innerHTML = "Lütfen gerçek bir tarih giriniz.";
               }
                
                
              else if(arr[2] < 1900) {
                  hata.innerHTML = "Lütfen gerçek bir yıl giriniz.";
               }  
                
             else{
                  hata.innerHTML = "";
               }  
                
            }                      
               )      
    
        </script>
  • 09-06-2021, 20:56:50
    #3
    Developer
    c-18 = bulunduğumuz yıl - 18
        $(function() {
            $( "#datepicker" ).datepicker({
                yearRange: '1940:c-18' ,
                changeYear: true
            });
        });