1

I am working on a HTML page and can't seem to figure out how to check if the user input has an integer in it. I have tried parseInt, userInput.charAt(i) in a for loop, isNaN. Nothing seems to be working.

This is my HTML

<label for="author" id="author">Favorite Book Author: </label>
<input type="text" name="author" id="authorText">

This is my JavaScript

function checkAuthor(){
    var userInput = $("authorText").value;
    for(var i = 0; i < userInput.length; i++)
    {
        if(userInput.charAt(i) <= 0 || userInput.charAt(i) > 0)
        {
            addError("There is a number in 'Favorite Book Author' input");
            return false;
        }
        else
            return true;
    }
}

I need to see if there is any integer that was entered by the user. If the input was "Mark5", "Ma5rk", "5", I need it to catch that 5 and display an error. Is there anyway that will work? I have a feeling that the .charAt is not working because it is recognizing the ints as strings, but I have tried to do a parseInt of every single

var x = parseInt(userInput.charAt(i));

inside the for loop, and then compared x inside the if statements. But nothing seemed to work

The addError function is this

function addError(text){
    var mylist = window.document.createElement("li");
    var myText = window.document.createTextNode(text);
    mylist.appendChild(myText);
    window.document.getElementByTagName("ul")[0].appendChild(mylist);
}
Programmer
  • 105
  • 2
  • 11

4 Answers4

0

You can use regular expression to search that.

var userInput = $("authorText").value;
if (userInput.search(/[^a-zA-Z]+/)) {
  addError("There is a number in 'Favorite Book Author' input");
return false.
}
Rakesh Yadav
  • 113
  • 7
0

You should check whether the char code between 48 and 57

function checkAuthor(){
    var userInput = $("authorText").value;
    for(var i = 0; i < userInput.length; i++)
    {
        var c = userInput.charAt(i).charCodeAt(0);
        if(c <= 57 && c >= 48)
        {
            addError("There is a number in 'Favorite Book Author' input");
            return false;
        }
           
    }
     return true;

}
Chien_Khmt
  • 257
  • 2
  • 8
0

jQuery and JavaScript is not Same.
You can't use value in a $() returned. instead use val() and also use # to use an ID as selector. use [0-9] to check whether a number is present or not.

function checkAuthor(){
    var userInput = $("#authorText").val(); // add # to get ID and val()
    var result = /[0-9]+/.test(userInput);
    result?alert("false"):void(0);
    console.clear();
    console.log(!result);
    return !result;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label for="author" id="author">Favorite Book Author: </label>
<input type="text" name="author" id="authorText" onblur="checkAuthor();" />
Sagar V
  • 12,158
  • 7
  • 41
  • 68
0

Since your looking for a single number, a RejExp test will likely be the easiest/cleanest method.

JQuery

function checkAuthor(){
    var userInput = $("#authorText").val();

    // check whether any char in userInput is a number. yes: true. no: false
    if(/[0-9]/.test(userInput)) {
        addError("There is a number in 'Favorite Book Author' input");
        return false;
    } else
        return true;
    }
}

function addError(text){
    var mylist = window.document.createElement("li");
    var myText = window.document.createTextNode(text);
    mylist.appendChild(myText);
    window.document.getElementByTagName("ul")[0].appendChild(mylist);
}

Vanilla JS

function checkAuthor(){
    var userInput = document.getElementById('#authorText').value;

    // check whether any char in userInput is a number. yes: true. no: false
    if(/[0-9]/.test(userInput)) {
        addError("There is a number in 'Favorite Book Author' input");
        return false;
    } else
        return true;
    }
}

function addError(text){
    var mylist = window.document.createElement("li");
    var myText = window.document.createTextNode(text);
    mylist.appendChild(myText);
    window.document.getElementByTagName("ul")[0].appendChild(mylist);
}
Isaac B
  • 695
  • 1
  • 11
  • 20