简体   繁体   English

如何防止通过表单输入提交字符

[英]How to prevent characters being submitted with form input

I'm in the process of creating a gambling website, but currently with the betting form input you can type a negative number etc (-100) and you will have 100 coins put into your balance. 我正在创建一个赌博网站,但是目前使用投注表格输入,您可以输入一个负数等(-100),您的余额中将有100个硬币。

I need to restrict the use of anything but number digits being used within the input. 我需要限制输入中使用的数字以外的任何数字的使用。 Currently my work around is blocking the user from typing in anything other than numbers but you are able to hop into the developer tools and insert the value manually. 目前,我的解决方法是阻止用户输入数字以外的任何内容,但是您可以跳入开发人员工具并手动插入值。

I believe i need to have an onSubmit validation for the input which says if anything but numbers are inserted do not allow the submit. 我相信我需要对输入进行onSubmit验证,该验证表明是否插入了除数字以外的任何内容,不允许提交。

I'm not sure how i will do this, thanks for the help! 我不确定我将如何做,谢谢您的帮助!

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;
}

}

Use ctype_digit() 使用ctype_digit()

//returns true
<?php
if (ctype_digit('123')) {
    echo 'true';
} else {
    echo 'false' ;
}
?>


//returns false
<?php
if (ctype_digit('-123')) {
    echo 'true';
} else {
    echo 'false' ;
}
?>

you can use this reguler expression to validate for numbers 您可以使用此调节器表达式来验证数字

samp html <button type="submit" onclick="myfunc()">Click Me!</button> 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;
}
} 
}

here 这里

  • / at both ends mark the start and end of the regex /在两端标记正则表达式的开始和结束
  • ^ and $ at the ends is to check the full string than for partial matches 末尾的^和$用于检查完整字符串,而不是部分匹配
  • d* looks for multiple occurrences of number charcters d *寻找多次出现的数字字符

if you want only positive numbers use this /^[+]?([0-9]+(?:[\\.][0-9]*)?|\\.[0-9]+)$/ 如果只想使用正数,则使用此/^[+]?([0-9]+(?:[\\.][0-9]*)?|\\.[0-9]+)$/

this jquery code can solve your problem 这个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();
            }
        })
    })

here the #submit is the id of submit button and #form is the id of the form to be submitted 这里的#submit是提交按钮的ID,而#form是要提交的表单的ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM