Form Validation working weirdly

Hi there, I make this validation but when I play with it by changing the inputs, it is performing weirdly, I tried to find the problem but could not, could anyone please help me to find out what the actual problem is? Please see the video, after changing the name multiple times it is acccepting the wrong input and letting the action file open. https://codepen.io/hamzacodepen951/pen/xxedjzJ
20 Replies
Hamza Naeem
Hamza Naeem3mo ago
it should not go to checkout page wheen I wrote hamza5 as input This is my first form validation, may be logic is poor.
ἔρως
ἔρως3mo ago
none of your inputs have a name you're also just rolling out your own validation, instead of using the built-in api
Hamza Naeem
Hamza Naeem3mo ago
ok, what should I do now?
ἔρως
ἔρως3mo ago
start over use names use the built-in html validation
Hamza Naeem
Hamza Naeem3mo ago
if you could share some tutorial I would be really tankful to you.
ἔρως
ἔρως3mo ago
i dont have any tutorial, but you can check the mdn website: https://developer.mozilla.org/en-US/docs/Learn/Forms/Form_validation
Norwyx
Norwyx3mo ago
Is this project frontend only? If so, just use HTML validation.
Hamza Naeem
Hamza Naeem3mo ago
no i will use of php as wlll for backend thats why doing frontend validation
ἔρως
ἔρως3mo ago
for the front end, just use the normal html validation and add your custom messages for php, you do the same work, not because you want to but because it's mandatory
Jochem
Jochem3mo ago
Front-end validation is a courtesy to your end user to make submitting the form easier. It doesn't have to be foolproof because it's very easy to circumvent using nothing but the devtools in the browser. Like Epic said, just use the HTML validation tools, then make sure your validation code on your backend is absolutely ironclad and bulletproof, because that's where you control the code and where you can be absolutely sure the code isn't tampered with or removed altogether
Hamza Naeem
Hamza Naeem3mo ago
so what I have done till now is fine and I do not have to worry about the Submit button for now as it will be managed properly on the backend side, this is what you are saying?
Jochem
Jochem3mo ago
It is still a good idea to use html validation over rolling your own
Hamza Naeem
Hamza Naeem3mo ago
<input type="text" class="text_input fluid" placeholder=" " id="Postcode" maxlength="5" pattern="\d*" title="Postcode must be number with 5 digits"> I think you are talking about this
ἔρως
ἔρως3mo ago
yes, but the pattern is wrong \d* means that 1 is valid, but 00001 is also valid also abcd0 is valid, as well as ab0cd even %&/(5 is valid ^\d{5}$ <-- this pattern should do it, but 00000 is valid also, don't forget to validate in javascript
Hamza Naeem
Hamza Naeem3mo ago
you mean abcd0 and ab0cd could be postcode as well?
ἔρως
ἔρως3mo ago
yes, and would validate as fine
Hamza Naeem
Hamza Naeem3mo ago
for now I am doing for my country here postal code is in numbers, is that fine?
ἔρως
ἔρως3mo ago
it depends on what EXACTLY you want
Hamza Naeem
Hamza Naeem3mo ago
for example
ἔρως
ἔρως3mo ago
^0*[1-9]\d{0,4}$ <-- here's a regex that accepts with or without leading 0, and requires 0-4 numbers after 1, 001, 00100, 00000000000000000000010000 are all valid but, since you limit the length to 5, you can only type 0000 before you need to put any other number before it isn't valid 0, 00, 000, 0000, 00000 and 00000000000000 is invalid as well the idea is simple: any number of 0 before [1-9], and then up to 4 digits