Jump to content

Recommended Posts

Okay I manage to get us phone number validation working, mainly from help in this forum.

 

Now I want to validate product serial numbers.

This is for practice or dummy project.

 

Lets say I have two products. One has a serial number of 2468 the other has a serial number of 1234.

 

I want to create  validation that will compare the number enter by the user to the two serial number I posted above. and if the number doesn't match and error message will be display in the form label. If the future I could have many more serial numbers.

 

The problem is I know nothing about data bases and would rather add the serial numbers directly in the code then placing them in a data base. so that when a user enter the numbers, the function will compare it to the number I programmed in the code.

 

What I have only validate the format of the number entered not and actual number that can be compared when the submit button is clicked.

 

This is what I have.

 

<div class="problemProduct">
  <?php

  $error = '';
  if(array_key_exists('serialNumber', $_POST))
  {
    if(!preg_match('/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/', $_POST['serialNumber']))
    {
      $error = 'Invalid Serial Number!';
    }
  }

?>
<label for="serialNumber"><span class="product_label">Serial Number.</span></label>
<input type="text" name="serialNumber" id="serial" class="serial_numberField" 
<?php if (isset($missing)) {
			  echo 'value="'.htmlentities($_POST['serialNumber']).'"';} ?> />
<span class="warning"> <?php echo $error; ?></span>
  </div>

 

Any help will be greatly appreciated.

 

IC

If you really truly want to hardcode serial numbers into your code, just do this:

 

<div class="problemProduct">
  <?php
  $serialNumbers = array('2468','1234');
  $error = (in_array($_POST['serialNumber'], $serialNumbers))? null : 'Invalid Serial Number!';
?>
<label for="serialNumber"><span class="product_label">Serial Number.</span></label>
<input type="text" name="serialNumber" id="serial" class="serial_numberField"
<?php if (isset($missing)) {
              echo 'value="'.htmlentities($_POST['serialNumber']).'"';} ?> />
<span class="warning"> <?php echo $error; ?></span>
  </div>

 

All you have to do is add more numbers to the array.  You don't need to check the format, as the in_array compares the posted info with what's in the string.

If you really truly want to hardcode serial numbers into your code, just do this:

 

<div class="problemProduct">
  <?php
  $serialNumbers = array('2468','1234');
  $error = (in_array($_POST['serialNumber'], $serialNumbers))? null : 'Invalid Serial Number!';
?>
<label for="serialNumber"><span class="product_label">Serial Number.</span></label>
<input type="text" name="serialNumber" id="serial" class="serial_numberField"
<?php if (isset($missing)) {
              echo 'value="'.htmlentities($_POST['serialNumber']).'"';} ?> />
<span class="warning"> <?php echo $error; ?></span>
  </div>

 

All you have to do is add more numbers to the array.  You don't need to check the format, as the in_array compares the posted info with what's in the string.

 

Looking good and exactly what I am looking to accomplish but when I load the page in the browser, I noticed it is already echoing a validation message, I change the message to "Serial Number Required!" How ever I would like this message to change if the user enters the wrong number and click the submit button. Like "Please enter valid serial number."

 

IC

 

The example you was given was bang on what you mean mate.

 

It is working well, however, I would rather the error message display only if the user leaves the field black and tried to submit the form. Currently If I load the page with the current code, the error or validation message is indicated even when the user have not entered anything.

 

It could be left this way but as part of the learning process I was wondering how I could accomplish that.

 

This is the current code:

 

<div class="problemProduct">
  <?php
  $serialNumbers = array('AB2468101214','1234');
  $error = (in_array($_POST['serialNumber'], $serialNumbers))? null : 'A valid Product Serail Number Required!';
?>
<label for="serialNumber"><span class="product_label">Serial Number.</span></label>
<input type="text" name="serialNumber" id="serial" class="serial_numberField"
<?php if (isset($missing)) {
              echo 'value="'.htmlentities($_POST['serialNumber']).'"';} ?> />
<span class="warning"> <?php echo $error; ?></span>
  </div>
  
[b]IC[/b]

change this line:

 $error = (in_array($_POST['serialNumber'], $serialNumbers))? null : 'A valid Product Serail Number Required!';

to:

 $error = (in_array($_POST['serialNumber'], $serialNumbers) && !empty($_POST['serialNumber']))? null : 'A valid Product Serail Number Required!';

 

(Added the !empty to make sure the field was at least filled in)

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.