downah Posted March 3, 2012 Share Posted March 3, 2012 I have a script running which will add users, although I want to check some fields like if the email is an actual email etc, and I'd like to do that clientside, ATM I've got this running but it doesn't actually check the email, and will still submit anything even if the whole form is blank, can someone help me out? <?php include "connect.php"; ?> <script type='text/javascript'> function formValidator(){ var email = document.getElementById('email'); if(emailValidator(email, "Please enter a valid email address")){ return true; } } } } } } return false; } function emailValidator(elem, helperMsg){ var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/; if(elem.value.match(emailExp)){ return true; }else{ alert(helperMsg); elem.focus(); return false; } } </script> <?php if(isset($_SESSION['email'])) { ?> <form onsubmit='return formValidator()' id='register' action='addeduser.php' method='post' accept-charset='UTF-8'> <fieldset > <legend>Add User</legend> <input type='hidden' name='submitted' id='submitted' value='1'/> <label for='firstname' >First Name*: </label><br> <input type='text' name='firstname' id='firstname' maxlength="50" /><br> <label for='lastname' >Sur Name*: </label><br> <input type='text' name='lastname' id='lastname' maxlength="50" /><br> <label for='email' >Email*:</label><br> <input type='text' name='email' id='email' maxlength="50" /><br> <label for='website' >Web Site*:</label><br> <input type='text' name='website' id='website' maxlength="50" /><br> <label for='password' >Password*:</label><br><br> <input type='password' name='password' id='password' maxlength="50" /><br> <label for='address1' >Address Line 1*:</label><br> <input type='text' name='address1' id='address1' maxlength="50" /><br> <label for='address2' >Address Line 2*: </label><br> <input type='text' name='address2' id='address2' maxlength="50" /><br> <label for='city' >City / Town*: </label><br> <input type='text' name='city' id='city' maxlength="50" /><br> <label for='county' >County*:</label><br> <input type='text' name='county' id='county' maxlength="50" /><br> <label for='postcode' >Postcode*:</label><br> <input type='text' name='postcode' id='postcode' maxlength="7" /><br> <label for='sex' >Sex M/F*:</label><br> <input type='text' name='sex' id='sex' maxlength="1" /><br> <label for='dob' >Date of Birth*:</label><br> <select name="dobmonth" id="dobmonth"> <option> - Month - </option> <option value="January">January</option> <option value="Febuary">Febuary</option> <option value="March">March</option> <option value="April">April</option> <option value="May">May</option> <option value="June">June</option> <option value="July">July</option> <option value="August">August</option> <option value="September">September</option> <option value="October">October</option> <option value="November">November</option> <option value="December">December</option> </select> <select name="dobday" id="dobday"> <option> - Day - </option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> <option value="15">15</option> <option value="16">16</option> <option value="17">17</option> <option value="18">18</option> <option value="19">19</option> <option value="20">20</option> <option value="21">21</option> <option value="22">22</option> <option value="23">23</option> <option value="24">24</option> <option value="25">25</option> <option value="26">26</option> <option value="27">27</option> <option value="28">28</option> <option value="29">29</option> <option value="30">30</option> <option value="31">31</option> </select> <select name="dobyear" id="dobyear"> <option> - Year - </option> <option value="2004">2004</option> <option value="2003">2003</option> <option value="2002">2002</option> <option value="2001">2001</option> <option value="2000">2000</option> <option value="1999">1999</option> <option value="1998">1998</option> <option value="1997">1997</option> <option value="1996">1996</option> <option value="1995">1995</option> <option value="1994">1994</option> <option value="1993">1993</option> <option value="1992">1992</option> <option value="1991">1991</option> <option value="1990">1990</option> <option value="1989">1989</option> <option value="1988">1988</option> <option value="1987">1987</option> <option value="1986">1986</option> <option value="1985">1985</option> <option value="1984">1984</option> <option value="1983">1983</option> <option value="1982">1982</option> <option value="1981">1981</option> <option value="1980">1980</option> <option value="1979">1979</option> <option value="1978">1978</option> <option value="1977">1977</option> <option value="1976">1976</option> <option value="1975">1975</option> <option value="1974">1974</option> <option value="1973">1973</option> <option value="1972">1972</option> <option value="1971">1971</option> <option value="1970">1970</option> <option value="1969">1969</option> <option value="1968">1968</option> <option value="1967">1967</option> <option value="1966">1966</option> <option value="1965">1965</option> <option value="1964">1964</option> <option value="1963">1963</option> <option value="1962">1962</option> <option value="1961">1961</option> <option value="1960">1960</option> <option value="1959">1959</option> <option value="1958">1958</option> <option value="1957">1957</option> <option value="1956">1956</option> <option value="1955">1955</option> <option value="1954">1954</option> <option value="1953">1953</option> <option value="1952">1952</option> <option value="1951">1951</option> <option value="1950">1950</option> <option value="1949">1949</option> <option value="1948">1948</option> <option value="1947">1947</option> <option value="1946">1946</option> <option value="1945">1945</option> <option value="1944">1944</option> <option value="1943">1943</option> <option value="1942">1942</option> <option value="1941">1941</option> <option value="1940">1940</option> <option value="1939">1939</option> <option value="1938">1938</option> <option value="1937">1937</option> <option value="1936">1936</option> <option value="1935">1935</option> <option value="1934">1934</option> <option value="1933">1933</option> <option value="1932">1932</option> <option value="1931">1931</option> <option value="1930">1930</option> <option value="1929">1929</option> <option value="1928">1928</option> <option value="1927">1927</option> <option value="1926">1926</option> <option value="1925">1925</option> <option value="1924">1924</option> <option value="1923">1923</option> <option value="1922">1922</option> <option value="1921">1921</option> <option value="1920">1920</option> <option value="1919">1919</option> <option value="1918">1918</option> <option value="1917">1917</option> <option value="1916">1916</option> <option value="1915">1915</option> <option value="1914">1914</option> <option value="1913">1913</option> <option value="1912">1912</option> <option value="1911">1911</option> <option value="1910">1910</option> <option value="1909">1909</option> <option value="1908">1908</option> <option value="1907">1907</option> <option value="1906">1906</option> <option value="1905">1905</option> <option value="1904">1904</option> <option value="1903">1903</option> <option value="1902">1902</option> <option value="1901">1901</option> <option value="1900">1900</option> </select><br><br> <label for='customerfeedback' >Customer Feedback (1-99)*:</label><br> <input type='text' name='customerfeedback' id='customerfeedback' maxlength="2" /><br> <label for='wgtffeedback' >WGTF Feedback (1-99)*:</label><br> <input type='text' name='wgtffeedback' id='wgtffeedback' maxlength="2" /><br> <input type='submit' name='Submit' value='Submit' /> </fieldset> </form> <?php } else { echo 'You can not do this if you are not logged in.'; } echo '<p><a href="index.php">home</a></p>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/258209-validating-form-am-i-missing-something/ Share on other sites More sharing options...
Pikachu2000 Posted March 3, 2012 Share Posted March 3, 2012 This isn't a PHP problem, although you need to make sure you also validate form data server-side as well. Moving to JS help. Quote Link to comment https://forums.phpfreaks.com/topic/258209-validating-form-am-i-missing-something/#findComment-1323581 Share on other sites More sharing options...
downah Posted March 3, 2012 Author Share Posted March 3, 2012 Appreciated Quote Link to comment https://forums.phpfreaks.com/topic/258209-validating-form-am-i-missing-something/#findComment-1323585 Share on other sites More sharing options...
S3cr3t Posted March 4, 2012 Share Posted March 4, 2012 Why so many } ? Put the JS code at the end of your form. Try it another way. Snippet from good old JS. <form action='submit.php' method='post' name='form'> ... <input type='button' name='submit' onclick='SubmitForm();' value='Add' /> </form> <script> function SubmitForm() { var bError = false; if(document.form.email.value == '') { bError = true; document.getElementById('email').style.color = '#f00'; alert('Wrong email...'); } if(bError == false) { document.form.submit(); } } </script> Quote Link to comment https://forums.phpfreaks.com/topic/258209-validating-form-am-i-missing-something/#findComment-1323736 Share on other sites More sharing options...
downah Posted March 4, 2012 Author Share Posted March 4, 2012 Oops I see that is not meant to be there but then how will I if email is fine go through with the validation of registering the user?(which is now happening on another php page) Quote Link to comment https://forums.phpfreaks.com/topic/258209-validating-form-am-i-missing-something/#findComment-1323797 Share on other sites More sharing options...
S3cr3t Posted March 4, 2012 Share Posted March 4, 2012 If there are no errors so bErrors == false then submit form. So just the normal way action='yourphpvalidationfile.php' gets the $_POST. Quote Link to comment https://forums.phpfreaks.com/topic/258209-validating-form-am-i-missing-something/#findComment-1323802 Share on other sites More sharing options...
downah Posted March 4, 2012 Author Share Posted March 4, 2012 I see thanks! So what would be the valid code, I can just put after the if - action="validation.php"; ? This is the code I am running atm to add a user, I really only care about the email and maybe if some fields atleast have a value. <?php include "connect.php"; echo '<p><a href="index.php">Home</a></p>'; if(isset($_SESSION['email'])) { ?> <link rel="stylesheet" type="text/css" href="view.css" media="all"> <script type="text/javascript" src="view.js"></script> <form onsubmit='return formValidator()' id='register' action='addeduser.php' method='post' accept-charset='UTF-8'> <body id="main_body" > <img id="top" src="top.png" alt=""> <div id="form_container"> <h1>Add a Member</h1> <form id="form_362567" class="appnitro" method="post" action=""> <div class="form_description"> <h2> Add a Member</h2> <p></p> </div> <ul > <li class="section_break"> <h3>Login Details</h3> <p></p> </li> <li id="li_2" > <label class="description" for="email">Email: </label> <div> <input id="email" name="email" class="element text medium" type="text" maxlength="255" value=""/> </div> </li> <li id="li_3" > <label class="description" for="password">Password (Must be between 6-50 characters) </label> <div> <input id="password" name="password" class="element text medium" type="password" maxlength="255" value=""/> </div> </li> <li id="li_4" > <label class="description" for="confirmpw">Confirm Password: </label> <div> <input id="confirmpw" name="confirmpw" class="element text medium" type="text" maxlength="255" value=""/> </div> </li> <li class="section_break"> <h3>Personal Details</h3> <p></p> </li> <li id="li_6" > <label class="description" for="element_6">Name: </label> <span> <input id="firstname" name= "firstname" class="element text" maxlength="255" size="8" value=""/> <label>First</label> </span> <span> <input id="lastname" name= "lastname" class="element text" maxlength="255" size="14" value=""/> <label>Last</label> </span> </li> <li id="li_7" > <label class="description" for="website">Web Site: </label> <div> <input id="website" name="website" class="element text medium" type="text" maxlength="255" value=""/> </div> </li> <li id="li_12" > </li> <li id="li_11" > <label class="description" for="element_11">Contact Details </label> <div> <input id="address1" name="address1" class="element text large" value="" type="text"> <label for="element_11_1">Street Address</label> </div> <div> <input id="address2" name="address2" class="element text large" value="" type="text"> <label for="element_11_2">Address Line 2</label> </div> <div class="left"> <input id="city" name="city" class="element text medium" value="" type="text"> <label for="element_11_3">City</label> </div> <div class="right"> <input id="phone" name="phone" class="element text medium" value="" type="text"> <label for="element_11_4">Phone Number</label> </div> <div class="left"> <input id="postcode" name="postcode" class="element text medium" maxlength="15" value="" type="text"> <label for="element_11_5">Postcode</label> </div> <div class="right"> <select class="element select medium" id="county" name="county"> <option value="" selected="selected"></option> <optgroup label="England"> <option>Bedfordshire</option> <option>Berkshire</option> <option>Bristol</option> <option>Buckinghamshire</option> <option>Cambridgeshire</option> <option>Cheshire</option> <option>City of London</option> <option>Cornwall</option> <option>Cumbria</option> <option>Derbyshire</option> <option>Devon</option> <option>Dorset</option> <option>Durham</option> <option>East Riding of Yorkshire</option> <option>East Sussex</option> <option>Essex</option> <option>Gloucestershire</option> <option>Greater London</option> <option>Greater Manchester</option> <option>Hampshire</option> <option>Herefordshire</option> <option>Hertfordshire</option> <option>Isle of Wight</option> <option>Kent</option> <option>Lancashire</option> <option>Leicestershire</option> <option>Lincolnshire</option> <option>Merseyside</option> <option>Norfolk</option> <option>North Yorkshire</option> <option>Northamptonshire</option> <option>Northumberland</option> <option>Nottinghamshire</option> <option>Oxfordshire</option> <option>Rutland</option> <option>Shropshire</option> <option>Somerset</option> <option>South Yorkshire</option> <option>Staffordshire</option> <option>Suffolk</option> <option>Surrey</option> <option>Tyne and Wear</option> <option>Warwickshire</option> <option>West Midlands</option> <option>West Sussex</option> <option>West Yorkshire</option> <option>Wiltshire</option> <option>Worcestershire</option> </optgroup> <optgroup label="Wales"> <option>Anglesey</option> <option>Brecknockshire</option> <option>Caernarfonshire</option> <option>Carmarthenshire</option> <option>Cardiganshire</option> <option>Denbighshire</option> <option>Flintshire</option> <option>Glamorgan</option> <option>Merioneth</option> <option>Monmouthshire</option> <option>Montgomeryshire</option> <option>Pembrokeshire</option> <option>Radnorshire</option> </optgroup> <optgroup label="Scotland"> <option>Aberdeenshire</option> <option>Angus</option> <option>Argyllshire</option> <option>Ayrshire</option> <option>Banffshire</option> <option>Berwickshire</option> <option>Buteshire</option> <option>Cromartyshire</option> <option>Caithness</option> <option>Clackmannanshire</option> <option>Dumfriesshire</option> <option>Dunbartonshire</option> <option>East Lothian</option> <option>Fife</option> <option>Inverness-shire</option> <option>Kincardineshire</option> <option>Kinross</option> <option>Kirkcudbrightshire</option> <option>Lanarkshire</option> <option>Midlothian</option> <option>Morayshire</option> <option>Nairnshire</option> <option>Orkney</option> <option>Peeblesshire</option> <option>Perthshire</option> <option>Renfrewshire</option> <option>Ross-shire</option> <option>Roxburghshire</option> <option>Selkirkshire</option> <option>Shetland</option> <option>Stirlingshire</option> <option>Sutherland</option> <option>West Lothian</option> <option>Wigtownshire</option> </optgroup> <optgroup label="Northern Ireland"> <option>Antrim</option> <option>Armagh</option> <option>Down</option> <option>Fermanagh</option> <option>Londonderry</option> <option>Tyrone</option> </optgroup> </select> <label for="element_11_6">County</label> </div> <br> <label for='dob' class="element text medium" >Date of Birth:</label><br> <select name="dobmonth" id="dobmonth"> <option> - Month - </option> <option value="January">January</option> <option value="Febuary">Febuary</option> <option value="March">March</option> <option value="April">April</option> <option value="May">May</option> <option value="June">June</option> <option value="July">July</option> <option value="August">August</option> <option value="September">September</option> <option value="October">October</option> <option value="November">November</option> <option value="December">December</option> </select> <select name="dobday" id="dobday"> <option> - Day - </option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> <option value="15">15</option> <option value="16">16</option> <option value="17">17</option> <option value="18">18</option> <option value="19">19</option> <option value="20">20</option> <option value="21">21</option> <option value="22">22</option> <option value="23">23</option> <option value="24">24</option> <option value="25">25</option> <option value="26">26</option> <option value="27">27</option> <option value="28">28</option> <option value="29">29</option> <option value="30">30</option> <option value="31">31</option> </select> <select name="dobyear" id="dobyear"> <option> - Year - </option> <option value="2004">2004</option> <option value="2003">2003</option> <option value="2002">2002</option> <option value="2001">2001</option> <option value="2000">2000</option> <option value="1999">1999</option> <option value="1998">1998</option> <option value="1997">1997</option> <option value="1996">1996</option> <option value="1995">1995</option> <option value="1994">1994</option> <option value="1993">1993</option> <option value="1992">1992</option> <option value="1991">1991</option> <option value="1990">1990</option> <option value="1989">1989</option> <option value="1988">1988</option> <option value="1987">1987</option> <option value="1986">1986</option> <option value="1985">1985</option> <option value="1984">1984</option> <option value="1983">1983</option> <option value="1982">1982</option> <option value="1981">1981</option> <option value="1980">1980</option> <option value="1979">1979</option> <option value="1978">1978</option> <option value="1977">1977</option> <option value="1976">1976</option> <option value="1975">1975</option> <option value="1974">1974</option> <option value="1973">1973</option> <option value="1972">1972</option> <option value="1971">1971</option> <option value="1970">1970</option> <option value="1969">1969</option> <option value="1968">1968</option> <option value="1967">1967</option> <option value="1966">1966</option> <option value="1965">1965</option> <option value="1964">1964</option> <option value="1963">1963</option> <option value="1962">1962</option> <option value="1961">1961</option> <option value="1960">1960</option> <option value="1959">1959</option> <option value="1958">1958</option> <option value="1957">1957</option> <option value="1956">1956</option> <option value="1955">1955</option> <option value="1954">1954</option> <option value="1953">1953</option> <option value="1952">1952</option> <option value="1951">1951</option> <option value="1950">1950</option> <option value="1949">1949</option> <option value="1948">1948</option> <option value="1947">1947</option> <option value="1946">1946</option> <option value="1945">1945</option> <option value="1944">1944</option> <option value="1943">1943</option> <option value="1942">1942</option> <option value="1941">1941</option> <option value="1940">1940</option> <option value="1939">1939</option> <option value="1938">1938</option> <option value="1937">1937</option> <option value="1936">1936</option> <option value="1935">1935</option> <option value="1934">1934</option> <option value="1933">1933</option> <option value="1932">1932</option> <option value="1931">1931</option> <option value="1930">1930</option> <option value="1929">1929</option> <option value="1928">1928</option> <option value="1927">1927</option> <option value="1926">1926</option> <option value="1925">1925</option> <option value="1924">1924</option> <option value="1923">1923</option> <option value="1922">1922</option> <option value="1921">1921</option> <option value="1920">1920</option> <option value="1919">1919</option> <option value="1918">1918</option> <option value="1917">1917</option> <option value="1916">1916</option> <option value="1915">1915</option> <option value="1914">1914</option> <option value="1913">1913</option> <option value="1912">1912</option> <option value="1911">1911</option> <option value="1910">1910</option> <option value="1909">1909</option> <option value="1908">1908</option> <option value="1907">1907</option> <option value="1906">1906</option> <option value="1905">1905</option> <option value="1904">1904</option> <option value="1903">1903</option> <option value="1902">1902</option> <option value="1901">1901</option> <option value="1900">1900</option> </select><br><br> </li> <li id="li_29" > <label class="description" for="element_29">Sex: </label> <div> <select class="element select medium" id="Sex" name="Sex"> <option value="" selected="selected"></option> <option value="Male" >Male</option> <option value="Female" >Female</option> </select> </div> </li> <li class="section_break"> <h3>Experience</h3> <p></p> </li> <li id="li_15" > <label class="description" for="element_15">Level: </label> <div> <input id="Level" name="Level" class="element text small" type="text" maxlength="255" value=""/> </div> </li> <li id="li_14" > <label class="description" for="element_14">Services Offered: </label> <div> <textarea id="services" name="services" class="element textarea medium"></textarea> </div> </li> <li id="li_16" > <label class="description" for="element_16">Philosophy: </label> <div> <textarea id="philosophy" name="philosophy" class="element textarea medium"></textarea> </div> </li> <li id="li_17" > <label class="description" for="element_17">Mission Statement: </label> <div> <textarea id="mission" name="mission" class="element textarea medium"></textarea> </div> </li> <li id="li_18" > <label class="description" for="element_18">Customer promises above WGTF promises: </label> <div> <textarea id="customerpromises" name="customerpromises" class="element textarea medium"></textarea> </div> </li> <li id="li_19" > <label class="description" for="element_19">Courses undertaken through WGTF: </label> <div> <textarea id="coursesundertaken" name="coursesundertaken" class="element textarea medium"></textarea> </div> </li> <li id="li_20" > <label class="description" for="element_20">Other Courses: </label> <div> <textarea id="othercourses" name="othercourses" class="element textarea medium"></textarea> </div> </li> <li id="li_21" > <label class="description" for="element_21">Other non-golf Certifications: </label> <div> <textarea id="nongolfcert" name="nongolfcert" class="element textarea medium"></textarea> </div> </li> <li id="li_30" > <label class="description" for="element_30">CRB Checked: </label> <div> <select class="element select small" id="crbcheck" name="crbcheck"> <option value="" selected="selected"></option> <option value="Yes" >Yes</option> <option value="No" >No</option> </select> </div> </li> <li id="li_31" > <label class="description" for="element_31">First Aid Certified: </label> <div> <select class="element select small" id="firstaid" name="firstaid"> <option value="" selected="selected"></option> <option value="Yes" >Yes</option> <option value="No" >No</option> </select> </div> </li> <li id="li_22" > <label class="description" for="element_22">Latest CPD: </label> <div> <textarea id="latestcpd" name="latestcpd" class="element textarea medium"></textarea> </div> </li> <li id="li_23" > <label class="description" for="element_23">CPD Goals: </label> <div> <textarea id="cpdgoals" name="cpdgoals" class="element textarea medium"></textarea> </div> </li> <li id="li_24" > <label class="description" for="element_24">Most Influential Coach or Player: </label> <div> <input id="mostinfluential" name="mostinfluential" class="element text medium" type="text" maxlength="255" value=""/> </div> </li> <li id="li_25" > <label class="description" for="element_25">Main Strengths: </label> <div> <textarea id="mainstrength" name="mainstrength" class="element textarea medium"></textarea> </div> </li> <li id="li_26" > <label class="description" for="element_26">Years' Golfing Experience: </label> <div> <input id="yeargolfexperience" name="yeargolfexperience" class="element text small" type="text" maxlength="255" value=""/> </div> </li> <li id="li_27" > <label class="description" for="element_27">Customer Feedback Rating: </label> <div> <input id="customerfeedback" name="customerfeedback" class="element text small" type="text" maxlength="255" value=""/> </div> </li> <li id="li_28" > <label class="description" for="element_28">WGTF Rating: </label> <div> <input id="wgtffeedback" name="wgtffeedback" class="element text small" type="text" maxlength="255" value=""/> </div> </li> <input type='submit' name='Submit' value='Submit' /> </form> <div id="footer"> </div> </div> <img id="bottom" src="bottom.png" alt=""> </body> <?php } else { echo 'You can not do this if you are not logged in.'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/258209-validating-form-am-i-missing-something/#findComment-1323825 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.