Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write code that deletes special characters and limits the length of user input

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to write code that deletes special characters and limits the length of user input". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

The copy code is as follows:

/ check for illegal characters and check character length

Function checkSpeChar (obj, byteLength, title) {

Var value = document.getElementById (obj) .value

Value = value.replace (/ (^\ s*) / g, "). Replace (/ (\ s *) / g,")

Var ret = (/ [- |; |, |\ / |\ (|) |\ [|\] |\} |\ {|% | @ |\ * |! |\'] / .test (value))

Var arr = ["@", "#", "$", "%", "^", "&", "*", "", "/", "\", "\", "", ".", ".", ":", "?", "[", "]", " ","-",", "(", ")", "_", "\", "~", "insert", "delete", "update", "select"]

Value = value.toLowerCase ()

For (var I = 0; I

< arr.length; i++) { if (value.indexOf(arr[i].toLowerCase()) >

= 0) {

Ret = true

}

}

If (ret = = true) {

Top.LSAlert ("illegal characters in the content, please re-enter it!")

Document.getElementById (obj). Value ='

Return

}

LimitLength (value, byteLength, title, obj)

}

/ / verify the input character length

Function limitLength (value, byteLength, title, attribute) {

Value = value.replace (/\ rCMG, ") .replace (/\ nUniqg,"); / / enter line feed to replace two spaces respectively

Var newvalue = value.replace (/ [^\ X00 -\ xff] / g, "* *")

Var length = newvalue.length

/ / when the number of bytes filled in is less than the set number of bytes

If (length * 1

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report