kenwvs Posted July 12, 2006 Share Posted July 12, 2006 I am trying to write a form validating script and have most of the functions figured out. I have a couple where I want the opportunity to leave it blank, but, if they choose to fill it in, I want it to meet certain requirements.Example. The field can be left blank, but, if they put anything in the field, it must be in the form of a email address. (I know how the format for an email is function checkEmail($email) { //Check Email Format$pattern = "/^[A-z0-9\._-]+". "@". "[A-z0-9][A-z0-9-]*". "(\.[A-z0-9_-]+)*". "\.([A-z]{2,6})$/";return preg_match ($pattern, $email);but how do I add that it can be blank also.The same thing except it would be in a dollar value, but could be blank.Thanks,Ken Quote Link to comment https://forums.phpfreaks.com/topic/14358-a-function-for-form-validating/ Share on other sites More sharing options...
zq29 Posted July 12, 2006 Share Posted July 12, 2006 You could do it this way:[code]function checkEmail($email) { //Check Email Format if(empty($email)) { return true; } else { $pattern = "/^[A-z0-9\._-]+" . "@" . "[A-z0-9][A-z0-9-]*" . "(\.[A-z0-9_-]+)*" . "\.([A-z]{2,6})$/"; return preg_match ($pattern, $email); }}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14358-a-function-for-form-validating/#findComment-56635 Share on other sites More sharing options...
kenwvs Posted July 12, 2006 Author Share Posted July 12, 2006 Thanks for the help. I can see how that will work, and will give it a try.The demo that I followed through had some instructions, but for a newbie like me, I am getting more and more confused by the minute. I am able to follow how writing a separate functions script will save time in the long run as I can reuse those functions for other purposes, but am not able to see how by listing the following on the same page as the form, it is able to know when to run, and what to look for, as their are no titles? Can you also please explain what isset means and how it works.[code]<?php/* validation.php */require_once ('functions.php');$valid = TRUE;if (isset ($_POST['submit'])) { foreach($_POST as $key=>$value) { $$key = $value; } $valid = $user = LettersAndDigits($id); $title = $Variable = ($item_title); $valid = $valid && $title; $category = $Variable = ($item_category);[/code]This is part of the form as well.[code]<body bgcolor="#F5F3F5"><p align="center"><b><font face="Arial" size="3" color="#0000FF">Quick Lister</font></b></p><p align="center"><b><font face="Arial" color="#FF0000">For Sale 4 U - Canada'sOnline Auction</font></b></p><p align="center"><font face="Arial" size="2"><b>Please note: </b>You maynot use characters such as $, #, ", &, *,/, etc. It is best to use normaltext and numerical characters only. </p><hr><form method="POST" action="validation.php"> <font size="2"> User ID </font> <font size="2"> <font face="Arial"><input type="text" name="id" size="12" value="<?= $id ?>"</font> <font size="2">You must use the <font color="#FF0000">same User ID</font> for all items you are uploading during this session.</font> <font size="2">(It does not have to be your regular username.)<BR> </font> <font face="Arial"><font size="2">Item Title </font> <font size="2"> <input type="text" name="item_title" size="68" value="<?= $item_title ?>"</font> <font size="2"> Item Category: </font><select size="1" name="item_category" value="<?= $item_category ?>"> <option value="000">Please Select Category</option> <option value="150">Antiques</option> <option value="18">Automobiles - Cars</option> <option value="75">Home and Garden - Lawnmowers</option> <option value="76">Home and Garden - Rototillers</option>[/code]Thanks,Ken Quote Link to comment https://forums.phpfreaks.com/topic/14358-a-function-for-form-validating/#findComment-56640 Share on other sites More sharing options...
brown2005 Posted July 12, 2006 Share Posted July 12, 2006 if (!eregi ("^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}$", $emailfield))that is what i use to check for an appropiate email address Quote Link to comment https://forums.phpfreaks.com/topic/14358-a-function-for-form-validating/#findComment-56641 Share on other sites More sharing options...
zq29 Posted July 12, 2006 Share Posted July 12, 2006 isset() is used to check if a variable has been set/initialised. It is used in the example you have given where both your form and its validation are in the same script. if you have a button with the name "submit" and run if(isset($_POST['submit'])), this checks if the button has been pressed, if so, it then goes on to validate the submitted fields. Quote Link to comment https://forums.phpfreaks.com/topic/14358-a-function-for-form-validating/#findComment-56649 Share on other sites More sharing options...
kenwvs Posted July 12, 2006 Author Share Posted July 12, 2006 Thank You for the explanation. It is starting to make a bit more sense to me know. I don't have it working yet, but I think I am getting closer (atleast I hope so) and I am starting to understand what some of the commands do, and how they work.If I need to build a function that relates to time, would it be best to have it validate that it allows numbers and the colon, or is there a way to validate to actual time?Ken Quote Link to comment https://forums.phpfreaks.com/topic/14358-a-function-for-form-validating/#findComment-56673 Share on other sites More sharing options...
GingerRobot Posted July 12, 2006 Share Posted July 12, 2006 What sort of time are you trying to validate? The function would depend on what the input is acually supposed to be. Quote Link to comment https://forums.phpfreaks.com/topic/14358-a-function-for-form-validating/#findComment-56684 Share on other sites More sharing options...
kenwvs Posted July 12, 2006 Author Share Posted July 12, 2006 I need to validate that the time is a valid time. It is the time an auction ends. It would always be on the hour (2:00 A.M. or 4:00 P.M.)Also, I am sure there must be an if variable. Example: If field A is #1, then you must have a valid answer in this field.Example: If the auction_type is Auction, then you must have a valid answer in this box. If the auction_type is Fixed Price, then this box may be empty.Thanks,Ken Quote Link to comment https://forums.phpfreaks.com/topic/14358-a-function-for-form-validating/#findComment-56814 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.