Jump to content

Input 21 numbers


Timlab55

Recommended Posts

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 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.