0

Below you will find my code for a form. I am trying to do Regular expression. I have the expression but when i enter something that is not right it will still post. I believe the reason is that i am still calling the $_POST of original not taking into consideration of the regex. Any help would be great.

<html> 
   <head> <title> Assign 2 </title> 
    <style type = "text/css"> 
            p.border 
            { 
            border-color: red; 
            border-style: solid; 
            border-width: 10px; 
            padding: 15px; 
            font-size: 65px; 
            color: blue; 
            text-align: center; 
            } 
    </style> 
    <script language="Javascript" type="text/javascript"> 

    function limitText(limitField, limitNum) { 
            if (limitField.value.length > limitNum) { 
        limitField.value = limitField.value.substring 
        (0, limitNum); 
    }  
       function check() 
        { 
            var csat = document.entryForm.major_CSAT; 
            var isas = document.entryForm.major_ISAS; 
             
            if(csat.checked == true) 
            { 
                enable('CSAT_area'); 
            } 
            else 
            { 
                disable('CSAT_area'); 
            } 
             
            if(isas.checked == true) 
            { 
                enable('ISAS_area'); 
            } 
            else 
            { 
                disable('ISAS_area'); 
            } 
        } 
         
        function disable(toDisable) 
        { 
            elements = document.entryForm.elements; 
            for(var i = 0; i < elements.length; i++) { 
                var elementName = elements[i].parentNode.getAttribute('name'); 
                if(elementName == toDisable) 
                { 
                    elements[i].disabled = true; 
                    elements[i].parentNode.style.color = '#999999'; 
                } 
            } 
        } 
         
        function enable(toEnable) 
        { 
            elements = document.entryForm.elements; 
            for(var i = 0; i < elements.length; i++) { 
                var elementName = elements[i].parentNode.getAttribute('name'); 
                if(elementName == toEnable) 
                { 
                    elements[i].disabled = false; 
                    elements[i].parentNode.style.color = '#000000'; 
                } 
            } 
        } 

    </script> 

   </head> 
   <body onload="check()"> 
        <form name="entryForm" action="crscat_resp.php" 
              method="post"> 
                <p class = "border"> 
                <b>Course Catalog Data Entry Page</b> 
                </P><br /> 

                Enter your first name: 
                <input type="text"name="fn"maxlength="20"/><br />     
             

                Enter your last name: 
                <input type="text"name="ln" maxlength="20"/>     
             
     
            <p> 
                Select the department abbreviation: 
                <select name="dept"> 
                <option value="ITEC">ITEC</option> 
                <option value="MATH">MATH</option> 
                <option value="STAT">STAT</option> 
                <option value="MGMT">MGMT</option> 
                <option value="MKTG">MKTG</option> 
                </select> 
            </p>         

             
                Enter the Course Number: 
                <input type="text"name="cn" maxlength="3"  
                size = "4"/><br />     
             
                Enter the Course Title: 
                <input type="text"name="ct" maxlength="30"  
                size = "40"/>     
             
             
            <p> 
                Enter the Course Description:  
                (max. 400 characters)<br /> 
                <textarea rows="10" cols="50"  
                name ="cd"  
                onKeyDown="limitText(this,400);"  
                onKeyUp="limitText(this,400);"> 
                </textarea> 
            </p> 

            <table border="1"> 
                <tr> 
                      <th colspan="2">Select the major(s) 
                    that require this course.</th> 
                </tr> 
                <tr> 
                      <td><input type="checkbox"name="major_CSAT" 
                    value="CSAT" onclick="check()"/>CSAT</td> 
                       
                    <td><input type="checkbox"name="major_ISAS" 
                    value="ISAS" onclick="check()"/>ISAS</td> 
                </tr> 
                <tr> 
                      <th colspan="2">Areas of  
                    Concentration.</th> 
                </tr> 
                <tr> 
                      <td rowspan ="4" name="CSAT_area"> 
                        <input type="checkbox"name="con[]" 
                        value="Compuer Science" /> 
                        Compuer Science<br /> 
                     
                        <input type="checkbox"name="con[]" 
                        value="Database" /> 
                        Database<br /> 
                     
                        <input type="checkbox"name="con[]" 
                        value="Software Engineering"  
                        />Software Engineering<br /> 
                     
                        <input type="checkbox"name="con[]" 
                        value="Networks"/>Networks<br /> 

                    </td> 
                      <td name="ISAS_area"> 
                         
                        <input type="checkbox"name="con[]" 
                        value="Information Systems"/> 
                        Information Systems<br /> 
                                          
                        <input type="checkbox"name="con[]" 
                        value="Web Development" /> 
                        Web Development<br /><br /><br /> 

                    </td> 
                </tr> 
            </table> 
             
            <p> 
                <input type="submit" value="Submit New Course Entry" /> 
                <input type="reset" value="Reset Form" /> 
            </p> 

         
        </form> 

     
                  <h2>Source Code</h2>  
                 <p>  
                    <?php       
                           show_source(__FILE__);  
                    ?>  
                </p>  

   </body> 
</html>
<?php  
    ini_set('display_errors',1);  
    error_reporting(E_ALL | E_STRICT);  
?>  

<html>  

<head> 
    <style type = "text/css"> 
    p.border 
    { 
    border-color: black; 
    border-style: solid; 
    } 
    p.tborder 
    { 
    border-color: green; 
    border-style: solid; 
    border-width: 10px; 
    padding: 15px; 
    font-size: 65px; 
    color: black; 
    text-align: center; 
    } 
    .error { color: red; } 
    </style> 
</head> 
   <body> 
             

                <?php 
                    $fn = trim($_POST['fn']); 
                    $ln = trim($_POST['ln']); 
                     $dept = trim($_POST['dept']);  
                    $cn = trim($_POST['cn']); 
                    $ct = trim($_POST['ct']);  
                    $cd = trim($_POST['cd']);  
                    $dn = $dept . ' ' . $cn; 

                    $okay = TRUE; 

                    if (empty($_POST['fn'])) { 
                       print '<p class="error">Please enter  
                    your first name.</p>'; 
                        $okay = FALSE; 
                    } 

                    if (preg_match("/[a-zA-Z]+/",$fn)); 
                    else print '<p class="error"> 
                    Please only use letters<br /></p>'; 
                     
                     
                     
                    if (empty($_POST['ln'])) { 
                       print '<p class="error">Please enter  
                    your last name.</p>'; 
                        $okay = FALSE; 
                    } 
                     
                    if (preg_match("/[a-zA-Z]+/",$ln)); 
                    else print '<p class="error"> 
                    Please only use letters<br /></p>'; 
                     
                     
                    if (empty($_POST['cn'])) { 
                       print '<p class="error">Please enter  
                    a course number.</p>'; 
                        $okay = FALSE; 
                    } 
     
                    if (preg_match("/^[0-9]{100,499}+/",$cn)); 
                    else print '<p class="error"> 
                    Please only use 100 - 499 <br /></p>'; 

                    if (empty($_POST['ct'])) { 
                       print '<p class="error">Please enter  
                    the course title.</p>'; 
                        $okay = FALSE; 
                    } 

                    if (empty($_POST['cd'])) { 
                       print '<p class="error">Please enter  
                    a course description.</p>'; 
                        $okay = FALSE; 
                    } 
     
                ?> 
            <p class ="tborder"> 
                <b>Course Catalog System</b> 
            </p> 
            <p class = "border"> 
                <?php 
                             
                print ("$dn <br />"); 
                                 
                if (preg_match("/[a-zA-Z]+/",$ct)); 
                else print 'Please only use letters<br />'; 

                print strtoupper("$ct"); 
                 
                ?> 
            </p> 


            <p class = "border"> 
                <?php 
                print ("<u>Course Description</u> <br />                  
                $cd"); 
                ?> 
            </p> 

                <?php 
                print("IT Majors and Areas of Concentration                  
                that require this course. <br />");  
                 
                //$majorCheck = $_POST["major_ISAS"].$_POST["major_CSAT"]; 
                 
                if(isset($_POST["con"]) && (isset($_POST["major_ISAS"]) || isset 
                ($_POST["major_CSAT"]))) 
                { 
                            $con = $_POST['con']; 
                    if(isset($_POST["major_ISAS"])) 
                    { 
                        $areaCount = 0; 
                        $majorAreas = Array("Information Systems","Web Development"); 

                        foreach($con as $mc) 
                        { 
                            if(in_array($mc, $majorAreas)) 
                            { 
                                print "ISAS-$mc<br />\n"; 
                                $areaCount++; 
                            } 
                        } 
                        if($areaCount == 0) 
                        { 
                             print ('<p class="error">Please choose select                  
                 an area of concentration for this major</p>'); 
                        } 
                    } 
                    if(isset($_POST["major_CSAT"])) 
                    { 
                        $areaCount = 0; 
                        $majorAreas = Array("Computer Science","Database", 
            "Software Engineering","Networks"); 
                         
                        foreach($con as $mc) 
                        { 
                            if(in_array($mc, $majorAreas)) 
                            { 
                                print "CSAT-$mc<br/ >\n"; 
                                $areaCount++; 
                            } 
                        } 
                        if($areaCount == 0) 
                        { 
                             print ('<p class="error">Please choose select                  
                an area of concentration for this major</p>'); 
                        } 
                    } 
                } 
                else 
                { 
                            print '<p class="error">Please select at 
                least one major and concentration!</p>'; 
                } 
                ?> 

                      <h2>Source Code</h2> 
                      <?php  
                    show_source(__FILE__);  
                       ?>  
  </body>  
</html>
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by timhysniu
0

If you don't want to submit a form when a certain condition is not met then what you want to do is add something like this to your submit button:

<input type="submit" ... onsubmit="javascript:return(some_javascript_function())" />

this javascript function would return false if conditions are not met (ie. form shouldnt be submitted) or true if you want it to submit.

As far as regex and the conditions, I am not sure what you wanted to check for... But what I would do anyway is install something like RegexBuddy. I find it's an awesome way to test with various regex patterns, and you also have plenty of regex templates that you can use...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.