mercredi 31 juillet 2019

Forcing length of txt box or variable

I have some javascript code. I want to put in some error checking but don't know where to start. I want, if the contents of val1 is not exactly 21 characters in length, to stop the script, or better still give a message box to the fact. Or maybe it is possible to force the input box to containing 21 characters? Sorry if its a silly question but I am new to this and still learning.

I set the size of the input box to 21 but this only increase the dimension so its still possible to enter more or less characters than this. I thought maybe some sort of if statement might do it but dont know how to do this and my attempts have been unsuccessful.

<script type='text/javascript'>
function screenshot(){
  html2canvas(document.body).then(function(canvas) {

    // Get base64URL
    var base64URL = canvas.toDataURL('image/jpeg').replace('image/jpeg', 'image/octet-stream');
    var val1 = $('#datepicker').val();

    // AJAX request
    $.ajax({
       url: 'ajaxfile.php',
       type: 'post',
       //data: {image: base64URL, name: datepicker},
       data: {image: base64URL, datepicker: val1},
       success: function(data){
       console.log('Upload successfully');
       }
    });
  });
}
</script>

No matter what I tried it stopped the script working altogether, even when val1 was 21 characters.

Aucun commentaire:

Enregistrer un commentaire