2017-02-02 4 views
1

Ich habe eine Tabelle entwickelt, in der Schüler den Betreff-Namen und die Zeichen eingeben müssen. Ich habe Eingabefeld nur für numerische nur von Javascript gemacht, aber ich bin nicht in der Lage, Validierung zu tun, so dass der Schüler nicht 0 als Zeichen eingeben kann.Markierungen sollten nicht Null sein in Eingabefeld

$("#insertbotheli13").click(function() { 
 
    $("#tablebotheli13").each(function() { 
 
     var tds = '<tr>'; 
 
     jQuery.each($('tr:last td', this), function() { 
 
      tds += '<td>' + $(this).html() + '</td>'; 
 
     }); 
 
     tds += '</tr>'; 
 
     if ($('tbody', this).length > 0) { 
 
      $('tbody', this).append(tds); 
 
     } else { 
 
      $(this).append(tds); 
 
     } 
 
    }); 
 
}); 
 

 
$(".allownumericwithoutdecimal").on("keypress keyup blur",function (event) {  
 
      $(this).val($(this).val().replace(/[^\d].+/, "")); 
 
      if ((event.which < 48 || event.which > 57)) { 
 
       event.preventDefault(); 
 
      } 
 
     }); 
 
\t \t 
 
\t \t
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> 
 

 
<table id="tablebotheli13" class="table table-striped table-hover"> 
 
\t \t \t \t \t \t \t \t <input type="button" class="btn green" value="Add New+" id="insertbotheli13"></input> 
 
    <thead> 
 
     <tr> 
 
     <th>Subject</th> 
 
\t \t <th> Marks</th> 
 
     </tr> 
 
    </thead> 
 
    <tbody> 
 
     <tr> 
 
      
 
      <td> 
 
       <input type="text" class="form-control subject1" name="subject1"> 
 
      </td> &nbsp;&nbsp;&nbsp;&nbsp; 
 
\t \t \t <td> 
 
       <input type="text" class="form-control marks1 allownumericwithoutdecimal" name="marks1"> 
 
      </td> 
 
    
 
     </tr> 
 
    </tbody> 
 
</table>

+0

Verwenden Sie '' – Hemal

Antwort

1

Sie könnten den Eingangswert prüfen, ob es gleich Null ist, dann entfernen Sie es, überprüfen Sie die folgenden Code-Schnipsel.

Auch die Ereignisse sollten mit der Veranstaltung Delegation angebracht werden, da Sie die Eingabe der dynamisch sind und fügen hinzu:

$("body").on("keypress keyup blur paste", ".allownumericwithoutdecimal", function(event) { 
    if($(this).val() == "0"){ 
     $(this).val($(this).val().replace(/0/g, "")); 
    } 

    if ((event.which < 48 || event.which > 57)) { 
     event.preventDefault(); 
    } 
}); 

Hoffnung, das hilft.

$("#insertbotheli13").click(function() { 
 
    $("#tablebotheli13").each(function() { 
 
    var tds = '<tr>'; 
 
    jQuery.each($('tr:last td', this), function() { 
 
     tds += '<td>' + $(this).html() + '</td>'; 
 
    }); 
 
    tds += '</tr>'; 
 
    if ($('tbody', this).length > 0) { 
 
     $('tbody', this).append(tds); 
 
    } else { 
 
     $(this).append(tds); 
 
    } 
 
    }); 
 
}); 
 

 
$("body").on("keypress keyup blur paste", ".allownumericwithoutdecimal", function(event) { 
 
    if($(this).val() == "0"){ 
 
    $(this).val($(this).val().replace(/0/g, "")); 
 
    } 
 

 
    if ((event.which < 48 || event.which > 57)) { 
 
    event.preventDefault(); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> 
 

 
<table id="tablebotheli13" class="table table-striped table-hover"> 
 
    <input type="button" class="btn green" value="Add New+" id="insertbotheli13"></input> 
 
    <thead> 
 
    <tr> 
 
     <th>Subject</th> 
 
     <th>Marks</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <tr> 
 

 
     <td> 
 
     <input type="text" class="form-control subject1" name="subject1"> 
 
     </td>&nbsp;&nbsp;&nbsp;&nbsp; 
 
     <td> 
 
     <input type="text" class="form-control marks1 allownumericwithoutdecimal" name="marks1"> 
 
     </td> 
 

 
    </tr> 
 
    </tbody> 
 
</table>

0

Try this:

if (Number(form.qty.value) > 0) { 
    // Only executes if value is a positive number. 
} 
0

nur eine Bedingung an keyup setzen und keypress Ereignis entfernen, da es jetzt keine Notwendigkeit, von

ist

$("#insertbotheli13").click(function() { 
 
    $("#tablebotheli13").each(function() { 
 
     var tds = '<tr>'; 
 
     jQuery.each($('tr:last td', this), function() { 
 
      tds += '<td>' + $(this).html() + '</td>'; 
 
     }); 
 
     tds += '</tr>'; 
 
     if ($('tbody', this).length > 0) { 
 
      $('tbody', this).append(tds); 
 
     } else { 
 
      $(this).append(tds); 
 
     } 
 
    }); 
 
}); 
 

 
$(".allownumericwithoutdecimal").on("keyup blur",function (event) { 
 
      $(this).val($(this).val().replace(/[^\d].+/, "")); 
 
    if(parseInt($(this).val()) <= 0) alert("Marks should be more than Zero"); 
 
      if ((event.which < 48 || event.which > 57)) { 
 
       event.preventDefault(); 
 
      } 
 
     });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> 
 

 
<table id="tablebotheli13" class="table table-striped table-hover"> 
 
\t \t \t \t \t \t \t \t <input type="button" class="btn green" value="Add New+" id="insertbotheli13"></input> 
 
    <thead> 
 
     <tr> 
 
     <th>Subject</th> 
 
\t \t <th> Marks</th> 
 
     </tr> 
 
    </thead> 
 
    <tbody> 
 
     <tr> 
 
      
 
      <td> 
 
       <input type="text" class="form-control subject1" name="subject1"> 
 
      </td> &nbsp;&nbsp;&nbsp;&nbsp; 
 
\t \t \t <td> 
 
       <input type="text" class="form-control marks1 allownumericwithoutdecimal" name="marks1"> 
 
      </td> 
 
    
 
     </tr> 
 
    </tbody> 
 
</table>


0

Ihr Javascript eine Klasse rufen allownumericwithoutdecimal .Wenn u die Klasse ändern Sie die Antwort bekommen. Der Code war:

$(".allownumericwithoutdecimal").on("keyup blur",function (event) { 
      $(this).val($(this).val().replace(/[^\d].+/, "")); 
    if(parseInt($(this).val()) <= 0) alert("Marks should be more than Zero"); 
      if ((event.which < 48 || event.which > 57)) { 
       event.preventDefault(); 
      } 
     }); 

und ersetzen Sie mit diesem Code:

$(".allownumericwithoutdecimal").keypress(function(event) { 

       if (!event.which || (49 <= event.which && event.which <= 57) || (48 == event.which && $(this).val())) { 
        /* */ 
       } else { 
        event.preventDefault(); 
       } 
      }); 

Es ist für mich gearbeitet.

HINWEIS: Auch sollten Sie versuchen, </input> schließendes Tag von Ihrem HTML zu löschen.

Verwandte Themen