search
HomeBackend DevelopmentPHP TutorialComprehensive example of php special character filtering

  1. //Filter special characters of string
  2. function checkform() {
  3. var username = document.theform.user_name.value;
  4. var realname = document.theform.real_name.value;
  5. var passwd = document.theform.passwd.value;
  6. var passwd2 = document.theform.passwd2.value;
  7. var email = document.theform.email.value;
  8. var question = document.theform.question.value;
  9. var answer = document.theform.answer.value;
  10. var address = document.theform.address.value;
  11. var phone = document.theform.phone.value;
  12. var answer = document.theform.answer.value;
  13. var checkdata = /< ;|>|'|;|&|#|"|'/;
  14. var checkusername = /[^0-9]/;
  15. var checkmail = /^([a-za-z0-9_-])+ @([a-za-z0-9_-])+(.[a-za-z0-9_-])+/;
  16. var checkphone = /[^0-9-]/;
  17. if ( username == "" ) {
  18. alert("Username cannot be empty!");
  19. return false;
  20. }
  21. if ( passwd == "" ) {
  22. alert("Password cannot be empty!");
  23. return false;
  24. }
  25. if ( realname == "" ) {
  26. alert("Real name cannot be empty!");
  27. return false;
  28. }
  29. if ( question == "" ) {
  30. alert("Password question cannot be empty!" ");
  31. return false;
  32. }
  33. if ( answer == "" ) {
  34. alert("The answer to the password question cannot be empty!");
  35. return false;
  36. }
  37. if ( email == "" ) {
  38. alert("e-mail cannot be empty!");
  39. return false;
  40. }
  41. if ( checkusername.test(username) ) {
  42. }else{
  43. alert("Username cannot be all numbers!");
  44. return false;
  45. }
  46. if ( checkdata.test(username) ) {
  47. alert("The username contains illegal characters, please do not use special characters!");
  48. return false;
  49. }
  50. if ( username.length > 28 || username.length alert("The username length does not meet the requirements [3-28 characters] username.length");
  51. return false;
  52. }
  53. if ( passwd != passwd2 ) {
  54. alert ("The passwords entered twice are inconsistent!");
  55. return false;
  56. }
  57. if ( passwd.length > 28 || passwd.length alert("The password length does not meet the requirements [5-28 characters]");
  58. return false;
  59. }
  60. if ( checkdata.test(realname) ) {
  61. alert("The real name contains illegal characters, please do not use special characters!");
  62. return false;
  63. }
  64. if ( realname.length > 28 || realname.length alert("The length of the real name does not meet the requirements [3-28 characters]");
  65. return false;
  66. }
  67. if ( question.length > ; 98 || question.length alert("The password question length does not meet the requirements [6-98 characters]");
  68. return false;
  69. }
  70. if ( answer.length > 98 || answer.length alert("The answer length does not meet the requirements [3-98 characters]");
  71. return false;
  72. } //Script Academy http://bbs.it-home.org
  73. if ( ! checkmail.test(email) ) {
  74. alert("e-mail contains illegal characters!");
  75. return false;
  76. }
  77. if ( email.length > 48 || email.length alert("The e-mail length does not meet the requirements [5-48 characters]");
  78. return false;
  79. }
  80. if ( checkdata.test(address) ) {
  81. alert("The contact address contains illegal characters! ");
  82. return false;
  83. }
  84. if ( address != "" && address.length > 48 ) {
  85. alert("The length of the contact address does not meet the requirements [within 48 characters]");
  86. return false;
  87. }
  88. if ( checkphone.test(phone) ) {
  89. alert("Contact number contains illegal characters!");
  90. return false;
  91. }
  92. if ( address != "" && ( phone.length > 18 || phone .length alert("The length of the contact number does not meet the requirements [7-18 characters]");
  93. return false;
  94. }
  95. }
Copy code

PHP utility function for filtering special characters Special character filtering method for php form submission html special character filter php class How to escape special characters in url links Detailed explanation of php special character escaping php filter parameter special characters anti-injection php method to filter illegal and special strings Examples of php special character processing functions



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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),