使用jQuery限制特殊字符

时间:2014-01-24 11:31:35

标签: jquery

如何限制文本字段中的特殊字符? 特殊字符在一个变量中分配

sample="`!@#$%^&*()"

应使用名字和姓氏

检查此样本变量值

HTML代码

<input type="text" name="Firstname" value="Firstname" id="firstname">

的jQuery

            if(!sample.test($("#firstname").val())){
                alert("Nickname can have only alphabets and numbers.");
            }

5 个答案:

答案 0 :(得分:1)

试试这个:

function isValid(str){
 return !/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(str);
}

if(!isValid($("#firstname").val())){
            alert("Nickname can have only alphabets and numbers.");
        }

OR

if(/^[a-zA-Z0-9- ]*$/.test($("#firstname").val()) == false) {
alert("Nickname can have only alphabets and numbers.");
}

以上内容仅允许字符串在a-z,A-Z,0-9范围内完全由字符组成,加上连字符和空格字符。包含任何其他字符的字符串将导致alert

答案 1 :(得分:0)

试试这个,

var alphanumers = /^[a-zA-Z0-9]+$/;
if(!alphanumers.test($("#firstname").val())){
    alert("Nickname can have only alphabets and numbers.");
}

演示: http://jsfiddle.net/Z3VVU/

答案 2 :(得分:0)

没有特殊字符正则表达式:/^\s*[a-zA-Z0-9,\s]+\s*$/

DEMO

$(document).ready(function () {
    var charReg = /^\s*[a-zA-Z0-9,\s]+\s*$/;
    $('.keyup-char').keyup(function () {
        $('span.error-keyup-1').hide();
        var inputVal = $(this).val();

        if (!charReg.test(inputVal)) {
            $(this).parent().find(".warning").show();
        } else {
            $(this).parent().find(".warning").hide();
        }

    });
});

答案 3 :(得分:0)

对于输入类型,您可以使用它(也可以防止复制粘贴):

jQuery(document).ready(function() {
  jQuery("#name_name").keyup(function(event) {
    name=jQuery("#input_name").val();
    name=name.replace(/[^a-zA-Z 0-9.]+/g, '');
    jQuery("#name_name").val(name);
  }
  );
}
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="textbox" id="name_name">

答案 4 :(得分:0)

function allowCharacters(evt, strAllowedChars) {
// eg. allowCharacters('0-9|-|,|(|)')  Will allow 0 to 9, -,()|
var charCode = (evt.which) ? evt.which : event.keyCode
var keycodeval = "";
var finalkeycode = "";
if (strAllowedChars != "" || strAllowedChars != null || strAllowedChars.indexOf("|") != -1) {
    XstrAllowedChars = strAllowedChars.split("|");
    for (strcount = 0; strcount <= XstrAllowedChars.length - 1 ; strcount++) {
        if (XstrAllowedChars[strcount].search("-") != -1) {
            substr = XstrAllowedChars[strcount].split("-");
            if (substr[0] != substr[1]) {
                if (substr[0].charCodeAt(0) <= substr[1].charCodeAt(0)) {
                    codeval = "( charCode >= " + substr[0].charCodeAt(0) + " && " + "charCode <= " + substr[1].charCodeAt(0) + " )";
                    keycodeval = keycodeval + "||" + codeval;
                }
                else {
                    codeval = "( charCode <=" + substr[0].charCodeAt(0) + " && " + "charCode >= " + substr[1].charCodeAt(0) + " )";
                    keycodeval = keycodeval + "||" + codeval;
                }
            }
            else if (substr[0] == "" && substr[1] == "") {
                keycodeval = keycodeval + "|| " + " charCode == 45 " + " || " + " charCode == 32";
            }
        }
        else {
            codeval = "( charCode == " + XstrAllowedChars[strcount].charCodeAt(0) + " )";
            keycodeval = keycodeval + "||" + codeval;
        }
    }
}
newval = keycodeval.slice(2, keycodeval.length);
var retrunflag = false;
if (eval(newval)) {
    //var charCode1 = (evt.which) ? evt.which : event.keyCode       
    if (event.keyCode != 60 && event.keyCode != 62 && event.keyCode != 94 && event.keyCode != 38) {
        retrunflag = true; //event.keyCode  = event.keyCode; 
    }
    else
        retrunflag = false; //event.keyCode  = 0;
}
else {
    retrunflag = false; //event.keyCode  = 0 ;
}
//alert(retrunflag);
return retrunflag;}

 function funRestrictSpecialChar(Obj, evt) {
var inputVal = $("#" + Obj.id).val();
re = /[`~!@#$%^&*()_|+\-=?;:'"<>\{\}\[\]\\\/]/gi;
var isSplChar = re.test(inputVal);
if (isSplChar) {
    var no_spl_char = inputVal.replace(/[`~!@#$%^&*()_|+\-=?;:'"<>\{\}\[\]\\\/]/gi, '');
    $("#" + Obj.id).val(no_spl_char);
} }