iconicCreator Posted February 1, 2009 Share Posted February 1, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/143408-validating-serial-product-serial-numbers/ Share on other sites More sharing options...
.josh Posted February 1, 2009 Share Posted February 1, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/143408-validating-serial-product-serial-numbers/#findComment-752230 Share on other sites More sharing options...
iconicCreator Posted February 1, 2009 Author Share Posted February 1, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/143408-validating-serial-product-serial-numbers/#findComment-752245 Share on other sites More sharing options...
iconicCreator Posted February 2, 2009 Author Share Posted February 2, 2009 To simplify things I would rather the error message display only after the user have click the submit button and left the field blank. IC Quote Link to comment https://forums.phpfreaks.com/topic/143408-validating-serial-product-serial-numbers/#findComment-752270 Share on other sites More sharing options...
redarrow Posted February 2, 2009 Share Posted February 2, 2009 The example you was given was bang on what you mean m8. Quote Link to comment https://forums.phpfreaks.com/topic/143408-validating-serial-product-serial-numbers/#findComment-752272 Share on other sites More sharing options...
iconicCreator Posted February 2, 2009 Author Share Posted February 2, 2009 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] Quote Link to comment https://forums.phpfreaks.com/topic/143408-validating-serial-product-serial-numbers/#findComment-752276 Share on other sites More sharing options...
Philip Posted February 2, 2009 Share Posted February 2, 2009 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) Quote Link to comment https://forums.phpfreaks.com/topic/143408-validating-serial-product-serial-numbers/#findComment-752310 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.