Results 1 to 8 of 8

Thread: trouble validating number fields

  1. #1
    Join Date
    Nov 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default trouble validating number fields

    Hi, I am working on a form where the date fields need to be required and i'd like to have verification that they are numbers. For some reason the first field will not validate, even when numbers are entered. It seems to get stuck there. Email verification is working just fine. Here is the code and a link to the site:
    Code:
    <?php
    $email_sent = false;
    $error_message = "";
    
    if(isset($_POST['email'])) {
    		 
    	// EDIT THE 2 LINES BELOW AS REQUIRED
    	$email_to = "kate@madisonandmi.com";
    	$email_subject = "Contact Form Submission";
    	 
    	function clean_string($string) {
    	  $bad = array("content-type","bcc:","to:","cc:","href");
    	  return str_replace($bad,"",$string);
    	}
    	 
    	function validate_form($email, $age_month, $age_day, $age_year) {
    		// Validates Email Exists
    		if(strlen($email) == 0) {
    			return 'The Email Address is required.';
    		}
    
    		// Validate Email is in the right format
    		$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    		if(!preg_match($email_exp, $email)) {
    			return 'The Email Address you entered does not appear to be valid.';
    		}
    		
    		// Validate month is a number
    		$number_exp = '/\d+/';
    		if(!preg_match($number_exp, $age_month)) {
    		  return 'Please enter the number of the month your child was born. ie. January = 01';
    		}
    		
    		// Validate day is a number
    		$number_exp = '/\d+/';
    		if(!preg_match($number_exp, $age_day)) {
    		return 'Please enter the day your child was born.';
    		}
    		
    				// Validate year is a number
    				$number_exp = '/\d+/';
    		if(!preg_match($number_exp, $age_year)) {
    			return 'Please enter the year your child was born. ie. 2007';
    		}
    	}
    
    	// Validation expected data exists
    	if(!isset($_POST['email']) ||
    		!isset($_POST['age_month']) ||
    		!isset($_POST['age_day']) ||
    		!isset($_POST['age_year'])) {
    		$error_message = 'We are sorry, but there appears to be a problem with the form you submitted.';       
    	}
    
    	$email = $_POST['email']; // required
    	$age_years = $_POST['age_month']; // required
    	$age_months = $_POST['age_day']; // required
    	$age_months = $_POST['age_year']; // required
    
    	$error_message = validate_form($email, $age_month, $age_day, $age_year);
    
    	if(strlen($error_message) == 0) {
    		// No errors, Send Email
    		$body = "Form details below.\n\n";
    		$body .= "Email: ".clean_string($email)."\n";
    		$body .= "Month: ".clean_string($age_month)."\n";
    		$body .= "Day: ".clean_string($age_day)."\n";
    		$body .= "Year: ".clean_string($age_year)."\n";
    		 
    		// create email headers
    		$headers = 'From: '.$email."\r\n".
    		'Reply-To: '.$email."\r\n" .
    		'X-Mailer: PHP/' . phpversion();
    		
    		$email_sent = @mail($email_to, $email_subject, $body, $headers);  
    		if (!$email_sent)
    		{
    			$error_message = "Failed sending email";
    		}
    	}
    }
    ?>
    
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <link rel="shortcut icon" href="http://www.haepibean.com/favicon.ico" type="image/x-icon">
        <link rel="icon" href="http://www.haepibean.com/favicon.ico" type="image/x-icon">
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Haepi Bean | Speech &amp; Language Development Tools for Babies &amp; Children | Ages 3 months - 5 Years</title>
        <meta name="description" content="Haepi Bean | Language &amp; Speech Development Tools for Babies &amp; Children | Ages 3 months - 5 Years" />
        <meta name="keywords" content="expressive language disorder, latetalker, speech and language goals, language development in toddlers" />
        <link href="style/style.css" rel="stylesheet" />
        <meta property="og:title" content="Haepi Bean | Language &amp; Speech Development Tools for Babies &amp; Children | Ages 3 months - 5 Years" />
        <meta property="og:type" content="website" />
        <meta property="og:url" content="http://www.haepibean.com" />
        <meta property="og:image" content="http://www.haepibean.com/images/hb-LOGO.png" />
        <meta property="og:description" content="Haepi Bean is a subscription-based service that targets babies' and childrens' speech, language, and feeding milestones. Monthly packages are sent with games, toys, activities which facilitate the development of these milestones along with helpful tips and age-appropriate strategies to support speech and language development. " />
        <meta property="og:site_name" content="Haepi Bean" />
        <meta property="fb:admins" content="831950129" />
     
    </head>
    <body>
    
      <?php include 'header.php'; ?>
    
    
            <div class="content">
            
    <table width="950px">
        <tr>
        <td style="padding:50px 60px 15px 60px">
        <center>
    <span class="orange_head">We expect to begin offering subscription services in Mid-2014. </span> 
    </center>
    
    
    
    
    <img src="images/joinus-pic.png" alt="speach development learning packages" align="left" style="margin-right:40px; margin-top:30px" />
    
    
    		<table cellpadding="15">
            <tr>
            <td>
            
    				<?php 
    			if(strlen($error_message) > 0) {
    			?>
    			<div class="error_panel">
    				Oops! These errors appear below:<br />&nbsp;<br />
    				  <div class="error_message"><?php echo $error_message; ?></div>
    				<br />Please fix these errors and <a href="http://www.haepibean.com/sign_up.php">try again</a>.<br />&nbsp;<br />
    			</div>
    			<?php	
    			}
    			else if ($email_sent) {
    			?>
    			<div class="success">
    					Thank you for your interest in Haepi Bean! <p>We will be in touch soon.  </p><br />
    			</div>
    			<?php 
    			} 
    			else {
    			?>
                <form name="dform" method="post" onsubmit="return formCheck(this);">
                         <p>
                    <span class="purple">Leave your information, and you will be contacted<br /> once we are up and running!</span>
                </p>
                   <p>email :
                                <input type="text" name="email" style="width: 215px; height: 15px; overflow: hidden; background-color: #ffffff; border: solid; border-width: 1px; border-color: #6986B7; font-family: Arial, Helvetica, sans-serif; color: #6986B7; font-size: 14; letter-spacing: 1px;" />
    
                                </p><p>
                                child’s birthday: 
                                &nbsp; 
                                <input type="text" name="age_month" maxlength="2" minlength="2" style="width: 20px; height: 15px; overflow: hidden; background-color: #ffffff; border: solid; border-width: 1px; border-color: #6986B7; font-family: Arial, Helvetica, sans-serif; color: #6986B7; font-size: 14; letter-spacing: 1px;" />
    							/
                                <input type="text" name="age_day" maxlength="2" minlength="2" style="width: 20px; height: 15px; overflow: hidden; background-color: #ffffff; border: solid; border-width: 1px; border-color: #6986B7; font-family: Arial, Helvetica, sans-serif; color: #6986B7; font-size: 14; letter-spacing: 1px;" />
    							/
                                <input type="text" name="age_year" maxlength="4" minlength="2" style="width: 40px; height: 15px; overflow: hidden; background-color: #ffffff; border: solid; border-width: 1px; border-color: #6986B7; font-family: Arial, Helvetica, sans-serif; color: #6986B7; font-size: 14; letter-spacing: 1px;" />
                               
                                </p>
                                <input type="image" src="images-splash/hb-learnmorebutton.png" value="submit" name="submit" style="border: none; background: #ffffff; margin-left: 0px;" />
                           
    	
                </form>
    			<?php 
    			}
    			?>
                </td></tr></table>
                
                <p><img src="images/excited_to_join.png" alt="we are excited for you to join the haepi bean family!" />
                </p></center>
    
        </td>
        </tr>
    
    </table>
       
    
    
    		
            </div>
    <?php include 'footer.php'; ?>
    </body>
    </html>
    http://www.haepibean.com/sign_up1.php
    And help would be so appreciated!!
    thanks!

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I will look into this more when I have more time. But, just in case this is just a simple matter of mistaken identity, I will tell you that all form element values (as well as all POST and GET values) are strings. Yes stings. Even if they are numbers, they're strings. In other words 2 is not 2 if it's a form element value, it's '2' a string representation of the number. So to validate a number as one of these values, you don't care if it's a string or not, it always will be a string. You just care if it's a string containing only characters that can be interpreted as numbers, or not.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Nov 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    I will look into this more when I have more time. But, just in case this is just a simple matter of mistaken identity, I will tell you that all form element values (as well as all POST and GET values) are strings. Yes stings. Even if they are numbers, they're strings. In other words 2 is not 2 if it's a form element value, it's '2' a string representation of the number. So to validate a number as one of these values, you don't care if it's a string or not, it always will be a string. You just care if it's a string containing only characters that can be interpreted as numbers, or not.
    Thank you. I don't totally understand this, but I'm more concerned with making these fields required vs. confirming they are numbers. I removed then number validation, but the required fields are not working either. the page is reposted at the same URL. thanks for your help!

  4. #4
    Join Date
    Nov 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I actually set it up now with drop down fields, but now the first value is the default. is there a way to make default blank and require visitors to pick a valid field?
    http://www.haepibean.com/sign_up2.php

    I cant post the code because i keep getting errors that it is too long.

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I see, there is a problem with:

    PHP Code:
        // Validation expected data exists
        
    if(!isset($_POST['email']) ||
            !isset(
    $_POST['age_month']) ||
            !isset(
    $_POST['age_day']) ||
            !isset(
    $_POST['age_year'])) {
            
    $error_message 'We are sorry, but there appears to be a problem with the form you submitted.';       
        } 
    It simply doesn't act as one might expect, in other words, regardless of whether or not those values are set to something as opposed to nothing (I'm thinking they are always set since they are fields in the form after all), !isset() never evaluates to true. Now getting the validate to a number part working properly* would solve that. But to answer your question - "How to at least require these fields?", change that part to (use empty to test them instead of isset):

    PHP Code:
        // Validation expected data exists
        
    if(empty($_POST['email']) ||
            empty(
    $_POST['age_month']) ||
            empty(
    $_POST['age_day']) ||
            empty(
    $_POST['age_year'])) {
            
    $error_message 'We are sorry, but there appears to be a problem with the form you submitted.';       
        } 

    *To get the validate to a number part to work, you would first uncomment this line (remove the // prefix):

    PHP Code:
        //$error_message = validate_form($email, $age_month, $age_day, $age_year); 
    If there are still problems validating to a number after that, let me know.
    Last edited by jscheuer1; 11-21-2014 at 03:49 AM. Reason: saw the main validate to a number problem
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Nov 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you! We actually decided it best to use drop downs, so I created empty fields for the top value, but when those are left blank, they still validate. I changed the 'isset' to 'empty' and without selecting a month, day, year, they are empty, but the form still submits without a warning message. Or perhaps it is reading it as an entry, even though it has no value?
    I posted the page at http://www.haepibean.com/sign_up2.php

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    OK, now I'm stumped a little because my test page is working, but clearly your page is not. Let's clean up the garbage stuff that probably isn't a problem, but might be. For the form, change:

    Code:
    <form name="dform" method="post" onsubmit="return formCheck(this);">
    to:

    Code:
    <form name="dform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    The formCheck function is not defined (so will be causing some error, so may as well get rid of it to fix that), and there is no action specified for the form. Using "<?php echo $_SERVER['PHP_SELF']; ?>" ensures the form will submit to itself. That's what you seem to want anyway, this just makes it cleaner - more certain.

    Double check that you have changed all the !isset to empty, and that you are not using an old version of the code by accident.

    After fixing those two or three things, clearing the cache and refreshing the page, see if that helps.

    If not, I will need to see the raw PHP code again (post it) as one cannot see that on the live page by using 'view source'.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Nov 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you so much for taking so much time to help me with this. I replaced the code, but the form still seems to submit without picking a drop-down field (with the default blank fields selected). Here is the code (Ive only included the head code because it was too long to post):
    Code:
    <?php
    $email_sent = false;
    $error_message = "";
    
    if(isset($_POST['email'])) {
    		 
    	// EDIT THE 2 LINES BELOW AS REQUIRED
    	$email_to = "kate@madisonandmi.com";
    	$email_subject = "Contact Form Submission";
    	 
    	function clean_string($string) {
    	  $bad = array("content-type","bcc:","to:","cc:","href");
    	  return str_replace($bad,"",$string);
    	}
    	 
    	function validate_form($email, $age_month, $age_day, $age_year) {
    		// Validates Email Exists
    		if(strlen($email) == 0) {
    			return 'The Email Address is required.';
    		}
    
    		// Validate Email is in the right format
    		$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    		if(!preg_match($email_exp, $email)) {
    			return 'The Email Address you entered does not appear to be valid.';
    		}
    		
    		
    	}
    
        // Validation expected data exists
        if(empty($_POST['email']) ||
            empty($_POST['age_month']) ||
            empty($_POST['age_day']) ||
            empty($_POST['age_year'])) {
            $error_message = 'We are sorry, but there appears to be a problem with the form you submitted.';       
        } 
    
    	$email = $_POST['email']; // required
    	$age_month = $_POST['age_month']; // required
    	$age_day = $_POST['age_day']; // required
    	$age_year = $_POST['age_year']; // required
    
    	$error_message = validate_form($email, $age_month, $age_day, $age_year);
    
    	if(strlen($error_message) == 0) {
    		// No errors, Send Email
    		$body = "Form details below.\n\n";
    		$body .= "Email: ".clean_string($email)."\n";
    		$body .= "Month: ".clean_string($age_month)."\n";
    		$body .= "Day: ".clean_string($age_day)."\n";
    		$body .= "Year: ".clean_string($age_year)."\n";
    		 
    		// create email headers
    		$headers = 'From: '.$email."\r\n".
    		'Reply-To: '.$email."\r\n" .
    		'X-Mailer: PHP/' . phpversion();
    		
    		$email_sent = @mail($email_to, $email_subject, $body, $headers);  
    		if (!$email_sent)
    		{
    			$error_message = "Failed sending email";
    		}
    	}
    }
    ?>

Similar Threads

  1. Script to post a number then another number
    By PatrikIden in forum Looking for such a script or service
    Replies: 4
    Last Post: 02-12-2012, 12:29 AM
  2. Replies: 7
    Last Post: 01-05-2011, 11:02 AM
  3. trouble validating a form
    By mcolton in forum HTML
    Replies: 4
    Last Post: 08-22-2009, 12:53 PM
  4. comparing and validating two fields?
    By sakib000 in forum PHP
    Replies: 5
    Last Post: 03-06-2008, 07:25 AM
  5. Fields are white; refresh = fields are yellow
    By mototrance in forum Other
    Replies: 3
    Last Post: 05-17-2007, 09:16 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •