简体   繁体   English

如何在Javascript中删除输入字段的特殊字符?

[英]How to remove special characters of input field in Javascript?

I have form with 4 fields (item_code, item_name, item_search, description) If user fill the first input field (item_code), at the end it automatically sets the same value for all the other three input field. 我有4个字段的表单(item_code,item_name,item_search,description)如果用户填写第一个输入字段(item_code),最后它会自动为所有其他三个输入字段设置相同的值。

While setting the value I want to remove special characters in one field(item search). 设置值时,我想删除一个字段中的特殊字符(项目搜索)。

So I tried "replace", I don't know what mistake am I doing because I'm a beginner. 所以我尝试“替换”,我不知道我做了什么错,因为我是初学者。

item_code: function(frm) {
if(!frm.doc.item_name)
frm.set_value("item_name", frm.doc.item_code);
if(!frm.doc.item_search)
frm.set_value("item_search", frm.doc.item_code.replace(/[^A-Z0-9]/ig, "_");
if(!frm.doc.description)
frm.set_value("description", frm.doc.item_code);
},

i checked replace function is not working properly. 我检查更换功能不能正常工作。 change your .replace(/[^A-Z0-9]/ig, "_") with below code. 使用以下代码更改.replace(/ [^ A-Z0-9] / ig,“_”)

.replace(/[^a-zA-Z ]/g, "") .replace(/ [^ a-zA-Z] / g,“”)

it will work. 它会工作。

 var string = 'A2.11^11*111"@'; string = string.replace(/[^0-9a-zA-Z]+/g,''); console.log(string); 

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

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