如何用js或则jquery过滤特殊字符

JavaScript04

如何用js或则jquery过滤特殊字符,第1张

1、jQuery使用正则匹配替换特殊字符

function RegeMatch(){

    var pattern = new RegExp("[~'!@#$%^&*()-+_=:]")  

    if($("#name").val() != "" && $("#name").val() != null){  

        if(pattern.test($("#name").val())){  

            alert("非法字符!")  

            $("#name").attr("value","")  

            $("#name").focus()  

            return false  

        }  

    }  

}

2、jQuery限制输入ASCII值

//数字0-9的ascii为48-57

//大写A-Z的ascii为65-90

//小写a-z的ascii为97-122

// ----------------------------------------------------------------------

// <summary>

// 限制只能输入数字和字母

// </summary>

// ----------------------------------------------------------------------

$.fn.onlyNumAlpha = function () {

    $(this).keypress(function (event) {

        var eventObj = event || e

        var keyCode = eventObj.keyCode || eventObj.which

        if ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122))

            return true

        else

            return false

    }).focus(function () {

        this.style.imeMode = 'disabled'

    }).bind("paste", function () {

        var clipboard = window.clipboardData.getData("Text")

        if (/^(\d|[a-zA-Z])+$/.test(clipboard))

            return true

        else

            return false

    })

}

//-----调用方法$("#文本框id").onlyNumAlpha()

3、js正则匹配过滤

function stripscript(s) 

    var pattern = new RegExp("[`~!@#$^&*()=|{}':',\\[\\].<>/?~!@#¥……&*()——|{}【】‘;:”“'。,、?]") 

    var rs = "" 

    for (var i = 0 i < s.length i++) { 

        rs = rs+s.substr(i, 1).replace(pattern, '') 

    } 

    return rs 

}

var str="__20__40__43__on__"

var strAry=str.split("__")//得到一个20 40 43的数组

for(var i=0i<strAry.lengthi++)

alert(strAry[i])

======================

要只过滤出数字,这样做

var str="__20__40__43__on__"

var r=str.replace(/[^0-9]+/g," ").split(" ")//将不是数字的字符替换为空格

for(var i=0i<r.lengthi++)

alert(r[i])

您好

js检查是否含有非法字符,js 正则过滤特殊字符

//正则

function trimTxt(txt){

 return txt.replace(/(^\s*)|(\s*$)/g, "")

}

 

/**

 * 检查是否含有非法字符

 * @param temp_str

 * @returns {Boolean}

 */

function is_forbid(temp_str){

    temp_str=trimTxt(temp_str)

temp_str = temp_str.replace('*',"@")

temp_str = temp_str.replace('--',"@")

temp_str = temp_str.replace('/',"@")

temp_str = temp_str.replace('+',"@")

temp_str = temp_str.replace('\'',"@")

temp_str = temp_str.replace('\\',"@")

temp_str = temp_str.replace('$',"@")

temp_str = temp_str.replace('^',"@")

temp_str = temp_str.replace('.',"@")

temp_str = temp_str.replace('',"@")

temp_str = temp_str.replace('<',"@")

temp_str = temp_str.replace('>',"@")

temp_str = temp_str.replace('"',"@")

temp_str = temp_str.replace('=',"@")

temp_str = temp_str.replace('{',"@")

temp_str = temp_str.replace('}',"@")

var forbid_str=new String('@,%,~,&')

var forbid_array=new Array()

forbid_array=forbid_str.split(',')

for(i=0i<forbid_array.lengthi++){

if(temp_str.search(new RegExp(forbid_array[i])) != -1)

return false

}

return true

}

---------------------

作者:dongsir 董先生

来源:董先生的博客

原文链接:js检查是否含有非法字符

版权声明:本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。转载时请标注:http://dongsir.cn/p/195