Home  >  Article  >  Web Front-end  >  js code that filters special characters input_javascript skills

js code that filters special characters input_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:21:37932browse
Copy code The code is as follows:

<script> <br>function TextValidate() { <br> var code; <br>var character; <br>var err_msg = "The folder name cannot contain one of the following characters: n \ / : * ? " < > | & , "; <br>if (document.all ) <br>{ <br>code = window.event.keyCode; <br>} <br>else { <br>code = arguments.callee.caller.arguments[0].which; <br>} <br> var character = String.fromCharCode(code); <br>var txt = new RegExp("[\*,\&,\\,\/,\?,\|,\:,\<,\>, "]"); <br>if (txt.test(character)) { <br>alert(err_msg); <br>if (document.all) { <br>window.event.returnValue = false; <br> } <br>else { <br>arguments.callee.caller.arguments[0].preventDefault(); <br>} <br>} <br>} <br></script>

asp:TextBox ID="txtFolderNameEng" CssClass="frmTxt" runat="server" MaxLength="200" onkeypress="TextValidate()" >

Use the above in the onkeypress event of the textbox Just use the method.

You can limit the input of *,&,,/,?,|,<,>. If you need to limit more special symbols, just add var txt = new RegExp("[\*,\ &,\\,\/,\?,\|,\:,\<,\>,"]");
.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn