特殊字符验证无法按预期工作 [英] Special characters validation not working as expected

查看:81
本文介绍了特殊字符验证无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将正则表达式验证应用于文本框用户控件,它仅在我在文本框中的文本末尾输入内容时才起作用。当我在文本中间某处键入某些内容时,它无效。



例如:嘿,伙计! (当我在文本末尾键入'!'时,我的代码工作正常)嘿!人! (但是当我输入整个文本后,我在文本中间的某处插入'!',不工作)



以下是我的代码。



任何帮助将不胜感激!谢谢!



我尝试了什么:



 $(  textarea [id $ =<%= TxtValue.ClientID%>])。keyup( function (){
var txt = $( this )。val();
var regex = new RegExp ' [^ 0-9a -zA-Z - ,_。\:\s] + $');
var vldttxt = regex.test(txt );
if (txt.length> 0 && vldttxt === true ){
alert( 错误 );
}
var noSpclChar = txt.replace(正则表达式, );
this .value = noSpclChar;
});

解决方案

textarea [id


=<%= TxtValue.ClientID%>])。keyup( function (){
var txt =


this )。val();
var regex = new RegExp ' [^ 0-9a-zA-Z - ,_ \:\s] +

I'm trying to apply Regular Expression Validation to a textbox User Control and it's only working when I enter something at the end of the text in the textbox. And when I type something somewhere in the middle of the text, it's not working.

For Example: Hey Man! (When I type '!' at the end of the text, my Code's working fine) Hey! Man! (But when I insert '!' somewhere in the middle of the text after the entire text is typed, not working)

Below is my Code.

Any help would be greatly appreciated! Thanks!

What I have tried:

$("textarea[id$=<%= TxtValue.ClientID %>]").keyup(function () {
            var txt = $(this).val();
            var regex = new RegExp('[^0-9a-zA-Z-,_.\:\s]+$');
            var vldttxt = regex.test(txt);
            if (txt.length > 0 && vldttxt === true) {
                alert("Error");
            }
            var noSpclChar = txt.replace(regex, "");
            this.value = noSpclChar;
        });

解决方案

("textarea[id


=<%= TxtValue.ClientID %>]").keyup(function () { var txt =


(this).val(); var regex = new RegExp('[^0-9a-zA-Z-,_.\:\s]+


这篇关于特殊字符验证无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆