numeric validation in javascript [duplicate]

0

I'm trying to validate a form. the user can only instroduz numerals values. I did the following but it is not working

function ValidateContactForm()
{
    var year = document.ContactForm.year;

  if (year.value == "")
{
    isNaN( document.myForm.edition.value)
    window.alert("Please enter year.");
    year.focus();
    return false;
}  
}

HTML code

<body>
        <?php include 'addDataAdmin.php';?>
        <form name="ContactForm" action="addDataAdmin.php" method="POST" enctype="multipart/form-data" autocomplete="off" onsubmit="return ValidateContactForm();">
            ISBN:<input type="text" name="ISBN">
            Author's Name:<input type="text" name="Authorsname">
            Title:<input type="text" name="Title">
            Edition:<input type="number" name="edition">
            Year:<input type="number" name="year">

            Category:
            <select name="category" size="1">
                <option value="computing">Computing</option>
                <option value="Romance">Romance</option>
                <option value="Fiction">Fiction</option>
                <option value="Non-Fiction">Non-Fiction</option>
            </select>
            <br />

            Publisher:<input type="text" name="publisher">
            Quantity-in-stock:<input type="number" name="quantityinstock">
            Price:<input type="number" name="price">
            <input type="file" name="fileToUpload" id="fileToUpload">
            <input type="submit" value="Upload Image" name="submit" formaction="/upload.php">
            <input type="submit" value="Send" name="send">
            <input type="reset" value="Clear">
        </form>
    </body>
</html>
    
asked by anonymous 20.04.2017 / 21:03

3 answers

2

SCRIPT

    function SomenteNumero(e){
      var tecla=(window.event)?event.keyCode:e.which;   
        if((tecla>47 && tecla<58)) return true;
        else{
           if (tecla==8 || tecla==0) return true;
        else  
           alert ( "Este campo aceita apenas números.");
           return false;
        }
    }

HTML

  

On inputs that should only accept numbers, place:

     

onkeypress = 'returnNumber (event)'

   <body>
        <?php include 'addDataAdmin.php';?>
        <form name="ContactForm" action="#" method="POST" enctype="multipart/form-data" autocomplete="off">
            ISBN:<input type="text" name="ISBN">
            Author's Name:<input type="text" name="Authorsname">
            Title:<input type="text" name="Title">
            Edition:<input type="number" name="edition" onkeypress='return SomenteNumero(event)'>
            Year:<input type="number" name="year" onkeypress='return SomenteNumero(event)'>

            Category:
            <select name="category" size="1">
                <option value="computing">Computing</option>
                <option value="Romance">Romance</option>
                <option value="Fiction">Fiction</option>
                <option value="Non-Fiction">Non-Fiction</option>
            </select>
            <br />

            Publisher:<input type="text" name="publisher">
            Quantity-in-stock:<input type="number" name="quantityinstock" onkeypress='return SomenteNumero(event)'>
            Price:<input type="number" name="price">
            <input type="file" name="fileToUpload" id="fileToUpload">
            <input type="submit" value="Upload Image" name="submit" formaction="/upload.php">
            <input type="submit" value="Send" name="send">
            <input type="reset" value="Clear">
        </form>
    </body>

Demo

function SomenteNumero(e){
    var tecla=(window.event)?event.keyCode:e.which;   
    if((tecla>47 && tecla<58)) return true;
    else{
    	if (tecla==8 || tecla==0) return true;
	else  
	alert ( "Este campo aceita apenas números.");
	return false;
    }
}
	
<input type="text" class="verificar" name="nome" size="2" onkeypress="return SomenteNumero(event)">
    
20.04.2017 / 21:47
1

You do not need Javascript for this, just go to your form and change the input to a type number (HTML5):

Example:

 <input type="number" size="6" name="age" min="18" max="99" value="21" required />
    
20.04.2017 / 21:08
0

If it's just validating if it's a valid number, you can do that too, similar to what you did.

Accepts both int and float , remembering that float will only work with . type 10.28 to accept , is already another problem.

function valida() {
    	var el = document.getElementById("ano");
    	var str = el.value;
    	var num = parseFloat(str);
      
      if(isNaN(num)){
        alert('Não é um número válido '+num);
      }else{
        alert('É um número válido!! '+num);
      }
	}
<input type="text" class="nome2" id="ano" value="">

<button type="button" onclick="valida()">OK</button>
    
21.04.2017 / 00:33