我想知道,确保用户进入投入箱的价值在网页上是数字的最好和最容易的方法是什么? 在通知他们时,不允许他们进入非数字价值。
是否有任何想法?
感谢
我想知道,确保用户进入投入箱的价值在网页上是数字的最好和最容易的方法是什么? 在通知他们时,不允许他们进入非数字价值。
是否有任何想法?
感谢
如果只允许煽动性价值观,我就试图做这样的尝试:
if( isNaN(parseInt(str, 10)) ) {
//error
}
EDIT: @M28 and @unomi are right, this might allow some inputs that aren t really numbers. M28 posted a really good solution in the comments:
if( ! (parseInt(str, 10).toString() == str) ) {
//error
}
您可定期使用 expressiond*美元
var re = new RegExp("^d*$");
if ($("#someinput").val().match(re)) {
alert("Only numbers");
} else {
alert("No match");
}
^ tells the expression to start matching from the beginning of the string.
d is a match for numbers which is the same as [0-9] (any number from 0 to 9)
* tells to match all numbers in string.
$ tells the expression to match until the end of the string.
The $("#someinput").val() is jquery but you could use normal javascript as in: document.somepintput.value.match(re).
定期表达是值得学习的,因为它们能够以冷静和简洁的方式解决许多问题。 http://en.wikipedia.org/wiki/Regular_expression”rel=“nofollow noreferer”>Regular expression
虽然Nc3b已经关闭,但使用 par子 确实允许你可能不想这样做。
parseInt("133t",10)
结果:133
做得更好
if(isNaN(Number(str, 10)){
Error;
}
如果你想确保整个扼杀行动必须代表若干人。
I use jquery and aphaNumeric plugin, see here for a demo
你们可以有一只手脚印,检查可能使用的是NAN功能,看所输入的数值是多少。
(单位:美元)
无
法典抓住了投入价值,然后从一开始便进行核对,{5}是数位数(这是任择的)。
function validzip(){
txt1 = document.FormName.InputName.value;
if (/^d{5}$/ .test(txt1) == false){
/* Simpler/longer alternative to this is: if ((/[0-9]/g .test(txt1) == false) || (txt1.length != 5)){ */
alert( Not Valid );
return false;
}
return true;
}
http://dataalbush.com
只允许输入数字。 使用和完全定制也非常容易,更不用提轻重。
在客户方面,从<代码><input category=" number”>开始; (该编码将作为type=“text”/code>在浏览器上处理,而浏览器尚未支持超文本5。 然后使用javascript(如他人所建议的)给非超5浏览器。
Don tabes-side accreditation in case people have javascript und.
Already some good answers have been posted from nc3b and skyfoot.
但nc3b 回答可能因投入制而失败,例如007
,并允许大量投入。
And skyfoot s response might not片面值 和 均允许空洞str光。
www.un.org/Depts/DGACM/index_spanish.htm 为了避免这些陷阱。 i 使用了以下代码。
$(document).ready(function(){
$("input").change(function(){
var enteredValue = $(this).val();
var re = new RegExp("^[-+]?[0-9]+$");
if (enteredValue != "" && re.test(enteredValue)) {
alert("You have entered valid integer");
} else {
alert("This is not valid integer");
}
//alert("The text has been changed.");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("input").change(function(){
var enteredValue = $(this).val();
var re = new RegExp("^[-+]?[0-9]+$");
if (enteredValue != "" && re.test(enteredValue)) {
alert("You have entered valid integer");
} else {
alert("This is not valid integer");
}
//alert("The text has been changed.");
});
});
</script>
</head>
<body>
<input type="text">
<p>Write something in the input field, and then press enter or click outside the field.</p>
</body>
</html>
I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....
I have a div <div id="masterdiv"> which has several child <div>s. Example: <div id="masterdiv"> <div id="childdiv1" /> <div id="childdiv2" /> <div id="...
I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...
<form><input type="file" name="first" onchange="jsFunction(2);"> <input type="file" name="second" onchange="jsFunction(3);"</form> Possible to pass just numbers to the js ...
So I ve got a menu with a hover/selected state and it loads fine in IE6/IE7. However when I scroll down the page and put the element outside of the viewport and then back in I get a broken image! I ...
I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...
Is it possible to reload a form after file-input change? I have a form where the user can chose an image for upload. I also have a php script which displays that image resized. I only wonder if it ...
I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!