Sunday, 15 June 2014

javascript - Require Specific First Characters from Form Post -



javascript - Require Specific First Characters from Form Post -

i'm putting out there see if can grab drift , give hand.. work public library , in process of making "self checkin" machine. here's code - there's form called "checkin" input called "barcode" - result of "barcode" posted php file sends info library scheme , receives message passed table "completed-checkins".

it's working fine - need set conditions on info accepted form.. conditions first 2 characters need t00 .

i've been searching web , trying borrow/adapt code others create work, got from;

<script type="text/javascript"> function checkcode() { var form = document.getelementbyid('checkin'); var x = form.elements.barcode.value.substring(0, 2); // var x = this.value.substring(0, 2); if (x == 't00') document.getelementbyid('#barcode').style.display = 'none'; } </script>

i'm no real coder wondering if can see how work? (it doesn't @ moment).

thanks much looking,

jordan.

and here's html scripts;

<body onload="$('#barcode').focus();" style="padding:40px;"> <center> <p> <img src="selfchecklogo.png" /> </p> <div class="formbarwrapper"> <div class="formbar"> <form method="post" name="checkin" id="checkin" onsubmit="return checkcode()" /> <input name="barcode" id="barcode" placeholder="scan item..." autocomplete="off" maxlength="9" /> </form> </div> </div> <div class="result"> <table id="completed-checkins"> <tbody> <tr> <td id="cell"></td> </tr> </tbody> </table> </div> </center> </body> <script type="text/javascript"> function checkcode() { var form = document.getelementbyid('checkin'); var x = form.elements.barcode.value.substring(0, 2); // var x = this.value.substring(0, 2); if (x == 't00') document.getelementbyid('#barcode').style.display = 'none'; } </script> <script type="text/javascript"> $(document).ready(function() { $('#checkin').submit(function() { $.post("index.php", { barcode: $('#barcode').val() }, function(data) { var content = ''; content += '<div class="result">'; content += '<tbody>'; content += '<tr>'; content += '<td>' + info + '</td>'; content += '</tr>'; content += '</tbody>'; content += '</div>'; $('#barcode').val('').focus(); $('#completed-checkins tbody').html(content); }); homecoming false; }); }); </script> </html>

have tried printing "x" console? think problem line

var x = form.elements.barcode.value.substring(0, 2);

only selects first 2 characters out of string, , compare "t00", 3 characters long , evaluate false.

also, might want consider using "===" operator instead of "==" operator. in case both work, using "===" habit in to. "===" works way expect work, whereas "==" has little gotchas in javascript. instance

0 == '0' //this evaluates true 0 === '0' //this evaluates false

basically, "==" doesn't check type, , "===" does.

javascript php forms validation input

No comments:

Post a Comment