0

I want an HTML Pattern attribute in order to have only letters A-Z, a-z, 0-9, and spaces.

I have the following which work, but the only problem is that, it also accepts it if I just enter a space and click submit.

[A-Za-z 0-9]+

For that reason I decided I am going to require atleast 3 characters, one of them being a letter. That lead me to write this:

[A-Za-z 0-9](?=.*[A-Za-z]).{3,}

But on this one, if I enter Jui;18 it accepts it. an since this is going to based into sql, that will create problems. Why does it accept the semicolon and what is the right pattern I need?

6
  • "based into sql"? What kind of problems? Not SQL injection, I hope; you should be using parameterized queries. Client-side validation like the pattern attribute wouldn’t help anyway. Commented Dec 29, 2017 at 7:13
  • @Ryan It is SQL injection because I have a PHP file that creates a database in the name entered in the text field. What do you recommend for me to solve the problem, then? Commented Dec 29, 2017 at 7:15
  • Why are you creating databases dynamically? But you would at least validate it on the server side with the same pattern. Test that this part is safe without the pattern attribute on the client, then add it. Commented Dec 29, 2017 at 7:17
  • I wouldn't allow a space in a database name. Commented Dec 29, 2017 at 7:18
  • @AndyG I am removing the Space in php side, but adding with slace into a table Commented Dec 29, 2017 at 17:45

1 Answer 1

1

Start with your pattern:

[A-Za-z 0-9]+

Require at least 3 characters:

[A-Za-z 0-9]{3,}

Require a letter:

(?=.*[A-Za-z])[A-Za-z 0-9]{3,}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.