简体   繁体   中英

limit user input by php

I have a username textbox. It should only accept between 3 and 10 characters and it should only accept (0, 9) and (A , Z ) values or maybe (- and _), nothing more
.

And if the user inputs a value like $%^# it should give an error
I want to do this with PHP, not Jquery. Can anyone help me with this?

I know I can use

<input name="1" type="text" value="1" maxlength="10" />

But it doesn't give an error to the user and just filters the input value.

Thank you.

regular expression could work

if (preg_match("%^[A-Za-z0-9-_]{3,10}$%", $_POST["1"])) {
    // OK
} else {
    // ERROR
}

When the value is submitted to your PHP script, you can look at it via the $_REQUEST global variable. In this case, you would reference $_REQUEST["1"] . To make sure the value it contains is of a certain size, use the strlen() function. To make sure that the value only contains certain characters, you can use preg_match_all() and an appropriate regular expression like the following:

[A-Za-z0-9]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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