试图清除javascript函数中文本框中的值

试图清除javascript函数中文本框中的值,javascript,jquery,asp.net,Javascript,Jquery,Asp.net,我正在尝试在KeyUp上的文本框上进行验证。文本框应仅包含5位数值,小数点后最多只能包含4位小数。例如,1234512345.2345 如果用户输入的值不是regex,那么texbox应该为空,我希望它在函数中完成,并且这个函数应该是通用的,以便其他任何人都可以使用这个函数 .Aspx 脚本函数 <script type="text/javascript"> function isFloatNumber(value) { var regex = /^[0-9]\d{0,4}(\

我正在尝试在KeyUp上的文本框上进行验证。文本框应仅包含5位数值,小数点后最多只能包含4位小数。例如,1234512345.2345 如果用户输入的值不是regex,那么texbox应该为空,我希望它在函数中完成,并且这个函数应该是通用的,以便其他任何人都可以使用这个函数 .Aspx


脚本函数

<script type="text/javascript">
function isFloatNumber(value) {
 var regex = /^[0-9]\d{0,4}(\.\d{1,4})?%?$/
            var regmatch = regex.test(value);
            if (regmatch == null|| regmatch==false) {
                alert("Please fil correct expression");
                value = "";
                return false;
            }
            return true;
        }
</script>

函数isFloatNumber(值){
var regex=/^[0-9]\d{0,4}(\.\d{1,4})?%$/
var regmatch=regex.test(值);
if(regmatch==null | | regmatch==false){
警告(“请填写正确的表达”);
value=“”;
返回false;
}
返回true;
}

您无法访问作为参数传递的值变量,它不引用输入框的值 相反,您可以访问元素并更改值,如下所示:


您无法访问作为参数传递的值变量,它不引用输入框的值 相反,您可以访问元素并更改值,如下所示:

函数为浮点数(elem){
var regex=/^[0-9]\d{0,4}(\.\d{1,4})?%$/
var regmatch=regex.test(元素值);
if(regmatch==null | | regmatch==false){
警告(“请填写正确的表达”);
elem.value=“”;
返回false;
}
返回true;
}

函数为浮点数(elem){
var regex=/^[0-9]\d{0,4}(\.\d{1,4})?%$/
var regmatch=regex.test(元素值);
if(regmatch==null | | regmatch==false){
警告(“请填写正确的表达”);
elem.value=“”;
返回false;
}
返回true;
}

更新值=”;不更新UI元素。您应该通过传递
this
来访问UI元素对象,并更新值,如
this.value=“”
,否则您应该使用诸如document.getElementbyId()之类的选择器来访问那些对象,如
document.getElementbyId('inSurfindN')。value=“”
更新值=“”;不更新UI元素。您应该通过传递
this
来访问UI元素对象,并更新值,如
this.value=“”
,否则您应该使用诸如document.getElementbyId()之类的选择器来访问那些对象,如
document.getElementbyId('inSurfindN')。value=“”

您可以使用下面的逻辑之一

函数为浮点数(obj){
var regex=/^[0-9]\d{0,4}(\.\d{1,4})?%$/
var regmatch=regex.test(对象值);
if(regmatch==null | | regmatch==false){
警告(“请填写正确的表达”);
obj.value=“”;
返回false;
}
返回true;
}

使用以下逻辑的方法之一

函数为浮点数(obj){
var regex=/^[0-9]\d{0,4}(\.\d{1,4})?%$/
var regmatch=regex.test(对象值);
if(regmatch==null | | regmatch==false){
警告(“请填写正确的表达”);
obj.value=“”;
返回false;
}
返回true;
}
var n=document.getElementById(“numPeople”),
r=document.getElementById(“结果”);
n、 addEventListener(“键控”,函数(e){
var regex=/^[0-9]\d{0,4}(\.\d{1,4})?%$/
var regmatch=regex.test(n.value);
if(regmatch==null | | regmatch==false){
警告(“请填写正确的表达”);
n、 值=“”;
返回false;
}
},假)
var n=document.getElementById(“numPeople”),
r=document.getElementById(“结果”);
n、 addEventListener(“键控”,函数(e){
var regex=/^[0-9]\d{0,4}(\.\d{1,4})?%$/
var regmatch=regex.test(n.value);
if(regmatch==null | | regmatch==false){
警告(“请填写正确的表达”);
n、 值=“”;
返回false;
}
},假)

您是否尝试了$('inpSurfIndN').val(“”)而不是value=“”?@JitendraRangpariya实际上我希望它是通用的,我的意思是,如果有五个不同的文本框,所有文本框都有相同的函数,那么我希望它是通用的。无需为不同的文本框键控事件编写不同的函数。您可以将“事件”参数传递给isFloatNumber函数,然后当它执行函数isFloatNumber时,eve参数包含调用函数的元素。请详细参考我的答案。您是否尝试了$('#inpSurfIndN').val(“”)而不是value=“”?@JitendraRangpariya实际上我希望它是通用的,我的意思是,如果有五个不同的文本框,所有文本框都有相同的函数,那么我希望它是通用的。无需为不同的文本框键控事件编写不同的函数。您可以将“事件”参数传递给isFloatNumber函数,然后当它执行函数isFloatNumber时,eve参数包含调用函数的元素。请详细参考我的回答。我想你需要的意思是
eve。currentTarget
函数是floatNumber(e)
。我想你需要的意思是
eve。currentTarget
函数是floatNumber(e)
<script type="text/javascript">
function isFloatNumber(value) {
 var regex = /^[0-9]\d{0,4}(\.\d{1,4})?%?$/
            var regmatch = regex.test(value);
            if (regmatch == null|| regmatch==false) {
                alert("Please fil correct expression");
                value = "";
                return false;
            }
            return true;
        }
</script>
 function isFloatNumber(eve) {
            var regex = /^[0-9]\d{0,4}(\.\d{1,4})?%?$/
            var regmatch = regex.test(value);
            if (regmatch == null|| regmatch==false) {
                alert("Please fil correct expression");
                var elem = eve.currentTarget;                 
                elem.value = "";
                return false;
            }
            return true;
        }