Timlab55 Posted October 2, 2021 Share Posted October 2, 2021 I'm sorry I started a war out there. Please forgive me. I ended up using Notepad ++. I've tried Visual Studio Code, I like it, but couldn't get it to do what I wanted. I want to write code and immediately see the results in a browsers. I also have XXAMP installed as well. But speaking of coding and this and that, I have a problem with something. What I would like is for a user to input 21 chars. ###-########-######-### However, this is what I have so far: PHP Part: if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["TicketNumber"])) { $TicketNumberErr = "21 Numbers is required "; }else{ $TicketNumber = test_input($_POST["TicketNumber"]); // check if name only contains letters and whitespace if(preg_match('/^\d{21}$/',$TicketNumber)){ echo "A match was found."; } else { $TicketNumberErr = "21 Numbers is required and do NOT include the hypen"; $TicketNumber=""; } } } HTML Part: Ticket Number: <input type="text" name="TicketNumber" size ="20" maxlength="21" value="<?php echo $TicketNumber ;?>"> <span class="error">* <?php echo $TicketNumberErr;?></span> <br><br> Test_Input Part: function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } The program does exactly what I want, however, it's rejecting the "-" or not counting them towards the 21. Can someone show me how to do this please? Thanks Dan Quote Link to comment https://forums.phpfreaks.com/topic/313857-input-21-numbers/ Share on other sites More sharing options...
requinix Posted October 4, 2021 Share Posted October 4, 2021 Your thread for choosing an editor might not have been the best place to ask for help with some code you've written, don't you think? Now that we're over here, // check if name only contains letters and whitespace if(preg_match('/^\d{21}$/',$TicketNumber)){ 1. That line of code does not check a "name" value, nor does it check if the whatever-value contains letters or whitespace. Letting code comments get out of date with the code they're describing is a great way to confuse yourself and other people. 2. That regular expression only allows for exactly 21 numbers. If you want to allow or require hyphens then you'll have to write something else. So what do you want to do? Require the hyphens? Make them optional? If they're optional, would it be valid for me to enter 1-2-3-4-5-6-7-8-9-0-1-2-3-4-5-6-7-8-9-0-1? What about 12345678901234567890-----------------------------------1? Come up with requirements for this number. Be as precise and explicit as you can be - because the code will reflect that. Quote Link to comment https://forums.phpfreaks.com/topic/313857-input-21-numbers/#findComment-1590646 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.