Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-03 07:38:21 11月14日 Update

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

Tags: Character input code length content byte special user more knowledge practical maximum successful next two dilemma base point actual situation article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Tech Info Huawei Docker Microsoft