Search code examples
javascriptjqueryhtmlfunctioninternet-explorer-6

JavaScript validating input only contains integers on submit


I've been trying for a bit to find a good way to go about this. Here's the trick this is being developed for IE6, so HTML5 is not supported (which is what is making this a pain, and why the button is actually a span). I need to allow all input into the input element but on submit i need to verify that the input is an integer and if it contains alpha characters or decimals throw an error.

 <table id="studentIdInputTable">
        <tr><td colspan="3"><hr class="style-hr" /></td></tr>

        <tr><td><span class="underline bold">Selection 1</span></td>
        <td class="center"><span class="small-text">Employee ID</span></td>
         </tr>
        <tr><td>Please enter your Student ID to <span class="bold italic">start your registration process</span></td>
        <td class="center"><input type="text" maxlength="11" id="tbNewStudentId" /></td>
        <td> <span id="btnNewStudent" class="validation-button">Start</span></td></tr>
   </table>

I have javascript native to the HTML page as below

function CheckNewStudentId(){
var newStudentID = $("tbNewStudentId").val();
newEmployeeID = parseInt(newEmployeeID, 10);
        var isNum = /^\d+$/.test(IDnumber);
        if (isNum == false) {
            alert(IDnumber + " is not a valid student number. Please enter only numbers 0-9, containing no alphabetical characters.");
        }
}

It would be easier if this was for a more updated browser platform. Any ideas how I can go about this?

Edit*** For reference to other users this was solved using this function

 function validIdCheck(Input){
 if(Number.isInteger(Input)==false){
alert("This is not a valid ID");
}
}

connected to this jQuery click function

 $("#btnNewStudent").click(function(){
   var newStuId = $("#tbNewStudentId").val();
   newStuId=parseInt(newStuId);
   validIdCheck(newStuId);
 });

Solution

  • To check if a number is an integer try this. It returns a boolean.

    Number.isInteger(yourNumber) 
    

    docs: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests