1

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);
 });
A. Morrow
  • 13
  • 1
  • 5
  • Possible duplicate of [Validate that a string is a positive integer](https://stackoverflow.com/questions/10834796/validate-that-a-string-is-a-positive-integer) – Heretic Monkey May 22 '17 at 14:51

2 Answers2

1

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

1

Using part of your code:

function validateStudentId(id) { return /^\d+$/.test(id) }

This will return true if it is a number or combination of numbers from 0 to 9.

If the id can not start with a 0, try this:

function validateStudentId(id) { return /^([1-9]|[1-9]\d+)$/ }

Quick explanation:

  1. ^ starts with
  2. [1-9] digit between 1 and 9
  3. | or
  4. \d digit (0 to 9)
  5. + occurs at least once and up to unlimited times
  6. $ ends with

Test it here

Doug
  • 5,661
  • 2
  • 26
  • 27