我正在创建一个赌博网站,但目前使用投注表格输入你可以输入一个负数等(-100),你将有100个硬币存入你的余额。
我需要限制使用除输入中使用的数字之外的任何数字。目前我的工作是阻止用户键入除数字以外的任何内容,但您可以跳到开发人员工具并手动插入值。
我相信我需要对输入进行onSubmit验证,该输入表示如果插入了数字,则不允许提交。
我不确定我将如何做到这一点,谢谢你的帮助!
答案 0 :(得分:0)
put onkeypress="return onKeyValidate(event,alpha);" on your input box,and
call the function
function onKeyValidate(e,charVal){
var keynum;
var keyChars = /[\x00\x08]/;
var validChars = new RegExp(charVal);
if(window.event)
{
keynum = e.keyCode;
}
else if(e.which)
{
keynum = e.which;
}
var keychar = String.fromCharCode(keynum);
if (!validChars.test(keychar) && !keyChars.test(keychar))
{
return false
} else{
return keychar;
}
}
答案 1 :(得分:0)
//returns true
<?php
if (ctype_digit('123')) {
echo 'true';
} else {
echo 'false' ;
}
?>
//returns false
<?php
if (ctype_digit('-123')) {
echo 'true';
} else {
echo 'false' ;
}
?>
答案 2 :(得分:0)
您可以使用此调节器表达式验证数字
samp html <button type="submit" onclick="myfunc()">Click Me!</button>
function myfunc(){
var a = /^\d*$/;
{
if(!a.test(value of input))
{
alert('Please provide a Number');
return false;
}
}
}
这里
如果您只想要正号,请使用此/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/
答案 3 :(得分:-1)
这个jquery代码可以解决你的问题
$(function(){
$("#submit").click(function(evnt){
var inptTxt = $("#myField").val();
if(check(inptTxt)){
//your test true then submits
$('#form').submit;
}else{ //test failed
evnt.preventDefault();
}
})
})
此处#submit
是提交按钮的ID,#form
是要提交的表单的ID