SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    form restrictions

    Hi i have got a form and limited it so you can only type numbers whitespaces and letters and it works but i found out if you copy and paste restricted characters inside the form it shows up how can i restrict that?

    Code:
    <?php
    include "firsttheme.php";
    ?>
    <img src="images/createaccountlarge.png">
    <font face="Arial" size="2">
    
    <form name="form1" method="post" action="createaccountauth.php">
    <script type='text/javascript'>function isEmpty(elem, helperMsg){    if(elem.value.length == 0){        alert(helperMsg);        elem.focus();        return true;    }    return false;}</script>
    <b>Personal Information</b></b><br>
    			<span style=color:'red'>*</span> First Name: <input type="text" id="REQ" name="firstname" id="FirstName" size="10"><br>
    			Surname: <input type="text" name="surname" id="LastName" size="10"><br>
    			<span style=color:'red'>*</span> Country:<select name="cr">
    <option selected value="Unknown">Choose One</option>
    <option value="Afghanistan">Afghanistan</option>
    <option value="Albania">Albania</option>
    <option value="Algeria">Algeria</option>
    <option value="Andorra">Andorra</option>
    <option value="Angola">Angola</option>
    <option value="Antigua and Barbuda">Antigua and Barbuda</option>
    <option value="Argentina">Argentina</option>
    <option value="Armenia">Armenia</option>
    <option value="Australia">Australia</option>
    <option value="Austria">Austria</option>
    <option value="Azerbaidjan">Azerbaidjan</option>
    <option value="Bahamas">Bahamas</option>
    <option value="Bahrain">Bahrain</option>
    <option value="Bangladesh">Bangladesh</option>
    <option value="Barbados">Barbados</option>
    <option value="Belarus">Belarus</option>
    <option value="Belgium">Belgium</option>
    <option value="Belize">Belize</option>
    <option value="Benin">Benin</option>
    <option value="Bermuda">Bermuda</option>
    <option value="Bhutan">Bhutan</option>
    <option value="Bolivia">Bolivia</option>
    <option value="Bosnia-Herzegovina">Bosnia-Herzegovina</option>
    <option value="Botswana">Botswana</option>
    <option value="Brazil">Brazil</option>
    <option value="Brunei Darussalam">Brunei Darussalam</option>
    <option value="Bulgaria">Bulgaria</option>
    <option value="Burkina Faso">Burkina Faso</option>
    <option value="Burundi">Burundi</option>
    <option value="Cambodia">Cambodia</option>
    <option value="Cameroon">Cameroon</option>
    <option value="Canada">Canada</option>
    <option value="Cape Verde">Cape Verde</option>
    <option value="Cayman Islands">Cayman Islands</option>
    <option value="Central African Republic">Central African Republic</option>
    <option value="Chad">Chad</option>
    <option value="Chile">Chile</option>
    <option value="China">China</option>
    <option value="Colombia">Colombia</option>
    <option value="Comoros">Comoros</option>
    <option value="Congo">Congo</option>
    <option value="Costa Rica">Costa Rica</option>
    <option value="Croatia">Croatia</option>
    <option value="Cuba">Cuba</option>
    <option value="Cyprus">Cyprus</option>
    <option value="Czech Republic">Czech Republic</option>
    <option value="Denmark">Denmark</option>
    <option value="Djibouti">Djibouti</option>
    <option value="Dominica">Dominica</option>
    <option value="Dominican Republic">Dominican Republic</option>
    <option value="Ecuador">Ecuador</option>
    <option value="Egypt">Egypt</option>
    <option value="El Salvador">El Salvador</option>
    <option value="Equatorial Guinea">Equatorial Guinea</option>
    <option value="Eritrea">Eritrea</option>
    <option value="Estonia">Estonia</option>
    <option value="Ethiopia">Ethiopia</option>
    <option value="Falkland Islands">Falkland Islands</option>
    <option value="Fiji">Fiji</option>
    <option value="Finland">Finland</option>
    <option value="France">France</option>
    <option value="Gabon">Gabon</option>
    <option value="Gambia">Gambia</option>
    <option value="Georgia">Georgia</option>
    <option value="Germany">Germany</option>
    <option value="Ghana">Ghana</option>
    <option value="Gibraltar">Gibraltar</option>
    <option value="Greece">Greece</option>
    <option value="Grenada">Grenada</option>
    <option value="Guam">Guam</option>
    <option value="Guatemala">Guatemala</option>
    <option value="Guinea">Guinea</option>
    <option value="Guinea Bissau">Guinea Bissau</option>
    <option value="Guyana">Guyana</option>
    <option value="Haiti">Haiti</option>
    <option value="Honduras">Honduras</option>
    <option value="Hong Kong">Hong Kong</option>
    <option value="Hungary">Hungary</option>
    <option value="Iceland">Iceland</option>
    <option value="India">India</option>
    <option value="Indonesia">Indonesia</option>
    <option value="Iran">Iran</option>
    <option value="Iraq">Iraq</option>
    <option value="Ireland">Ireland</option>
    <option value="Israel">Israel</option>
    <option value="Italy">Italy</option>
    <option value="Ivory Coast">Ivory Coast</option>
    <option value="Jamaica">Jamaica</option>
    <option value="Japan">Japan</option>
    <option value="Jordan">Jordan</option>
    <option value="Kazakhstan">Kazakhstan</option>
    <option value="Kenya">Kenya</option>
    <option value="Kuwait">Kuwait</option>
    <option value="Kyrgyzstan">Kyrgyzstan</option>
    <option value="Laos">Laos</option>
    <option value="Latvia">Latvia</option>
    <option value="Lebanon">Lebanon</option>
    <option value="Lesotho">Lesotho</option>
    <option value="Liberia">Liberia</option>
    <option value="Libya">Libya</option>
    <option value="Liechtenstein">Liechtenstein</option>
    <option value="Lithuania">Lithuania</option>
    <option value="Luxembourg">Luxembourg</option>
    <option value="Macedonia">Macedonia</option>
    <option value="Madagascar">Madagascar</option>
    <option value="Malawi">Malawi</option>
    <option value="Malaysia">Malaysia</option>
    <option value="Maldives">Maldives</option>
    <option value="Mali">Mali</option>
    <option value="Malta">Malta</option>
    <option value="Marshall Islands">Marshall Islands</option>
    <option value="Mauritania">Mauritania</option>
    <option value="Mauritius">Mauritius</option>
    <option value="Mexico">Mexico</option>
    <option value="Micronesia">Micronesia</option>
    <option value="Moldavia">Moldavia</option>
    <option value="Monaco">Monaco</option>
    <option value="Mongolia">Mongolia</option>
    <option value="Morocco">Morocco</option>
    <option value="Mozambique">Mozambique</option>
    <option value="Namibia">Namibia</option>
    <option value="Nepal">Nepal</option>
    <option value="Netherlands">Netherlands</option>
    <option value="New Zealand">New Zealand</option>
    <option value="Nicaragua">Nicaragua</option>
    <option value="Niger">Niger</option>
    <option value="Nigeria">Nigeria</option>
    <option value="North Korea">North Korea</option>
    <option value="Norway">Norway</option>
    <option value="Oman">Oman</option>
    <option value="Pakistan">Pakistan</option>
    <option value="Palau">Palau</option>
    <option value="Panama">Panama</option>
    <option value="Papua New Guinea">Papua New Guinea</option>
    <option value="Paraguay">Paraguay</option>
    <option value="Peru">Peru</option>
    <option value="Philippines">Philippines</option>
    <option value="Poland">Poland</option>
    <option value="Portugal">Portugal</option>
    <option value="Qatar">Qatar</option>
    <option value="Republic of Ireland">Republic of Ireland</option>
    <option value="Reunion (French)">Reunion (French)</option>
    <option value="Romania">Romania</option>
    <option value="Russian Federation">Russian Federation</option>
    <option value="Rwanda">Rwanda</option>
    <option value="Saint Kitts & Nevis Anguilla">Saint Kitts &amp; Nevis Anguilla</option>
    <option value="Saint Lucia">Saint Lucia</option>
    <option value="Saint Tome and Principe">Saint Tome and Principe</option>
    <option value="Saint Vincent & Grenadines">Saint Vincent &amp; Grenadines</option>
    <option value="Samoa">Samoa</option>
    <option value="San Marino">San Marino</option>
    <option value="Saudi Arabia">Saudi Arabia</option>
    <option value="Senegal">Senegal</option>
    <option value="Seychelles">Seychelles</option>
    <option value="Sierra Leone">Sierra Leone</option>
    <option value="Singapore">Singapore</option>
    <option value="Slovakia">Slovakia</option>
    <option value="Slovenia">Slovenia</option>
    <option value="Solomon Islands">Solomon Islands</option>
    <option value="Somalia">Somalia</option>
    <option value="South Africa">South Africa</option>
    <option value="South Korea">South Korea</option>
    <option value="Spain">Spain</option>
    <option value="Sri Lanka">Sri Lanka</option>
    <option value="Sudan">Sudan</option>
    <option value="Suriname">Suriname</option>
    <option value="Swaziland">Swaziland</option>
    <option value="Sweden">Sweden</option>
    <option value="Switzerland">Switzerland</option>
    <option value="Syria">Syria</option>
    <option value="Tadjikistan">Tadjikistan</option>
    <option value="Taiwan">Taiwan</option>
    <option value="Tanzania">Tanzania</option>
    <option value="Thailand">Thailand</option>
    <option value="Togo">Togo</option>
    <option value="Trinidad and Tobago">Trinidad and Tobago</option>
    <option value="Tunisia">Tunisia</option>
    <option value="Turkey">Turkey</option>
    <option value="Turkmenistan">Turkmenistan</option>
    <option value="Uganda">Uganda</option>
    <option value="United Kingdom">United Kingdom</option>
    <option value="United Arab Emirates">United Arab Emirates</option>
    <option value="Uruguay">Uruguay</option>
    <option value="USA">USA</option>
    <option value="Uzbekistan">Uzbekistan</option>
    <option value="Vanuatu">Vanuatu</option>
    <option value="Vatican City">Vatican City</option>
    <option value="Venezuela">Venezuela</option>
    <option value="Vietnam">Vietnam</option>
    <option value="Virgin Islands (British)">Virgin Islands (British)</option>
    <option value="Yemen">Yemen</option>
    <option value="Yugoslavia">Yugoslavia</option>
    <option value="Zaire">Zaire</option>
    <option value="Zambia">Zambia</option>
    <option value="Zimbabwe">Zimbabwe</option>
        </select><br>
    			State: <input type="text" name="st" id="State" size="20"><br>
    			City: <input type="text" name="city" id="City" size="20"><p>
    <b>Information About Myself</b><br>
    <span style=color:'red'>*</span> Gender:  <input type="radio" value="Male" name="gs" id="REQ">Male<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="radio" name="gs" value="Female">Female<br>
    <span style=color:'red'>*</span> Birthday:  <select name="day" id="REQ">
    <option value="pd">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="month">
    <option value="None">Month</option>
    <option value="January">01</option>
    <option value="February">02</option>
    <option value="March">03</option>
    <option value="April">04</option>
    <option value="May">05</option>
    <option value="June">06</option>
    <option value="July">07</option>
    <option value="August">08</option>
    <option value="September">09</option>
    <option value="October">10</option>
    <option value="November">11</option>
    <option value="December">12</option>
    </select><font size="2">
    <input type="text" name="year" id="Year" size="4"><br>
    Occupation:  <select name="occupation">
    <option value="pd">Prefer not to disclose</option>
    <option value="af">Accouting/Finance</option>
    <option value="cro">Computer related (other)</option>
    <option value="cri">Computer related (Internet)</option>
    <option value="c">Consulting</option>
    <option value="css">Customer service/support</option>
    <option value="et">Education/training</option>
    <option value="e">Engineering</option>
    <option value="esm">Execuitive/senior management</option>
    <option value="gas">General administrative/supervisory</option>
    <option value="gm">Government/military</option>
    <option value="h">Homemaker</option>
    <option value="mpo">Manufacturing/production/operations</option>
    <option value="pmle">Professional (medical, legal, ect.)</option>
    <option value="rad">Research and development</option>
    <option value="r">Retired</option>
    <option value="sma">Sales/marketing/advertising</option>
    <option value="sme">Self-employed/owner</option>
    <option value="s">Student</option>
    <option value="tc">Tradesman/craftsman</option>
    <option value="ubj">Unemployed/between jobs</option>
    <option value="o">Other</option>
    </select></p>
    <p>
    <b>Account Information</b><br>
    <span style=color:'red'>*</span> Email:
    <input type="text" name="email" id="Email" size="20" id="REQ"><br>
    <span style=color:'red'>*</span> Username:
    <input type="text" name="username" id="REQ" value="<?= $username ?>" size="15"><br>
    <span style=color:'red'>*</span> Password:
    <input type="password" name="password" id="REQ" size="10"><p>
    
    <input type='submit' onclick="isEmpty(document.getElementById('REQ'), 'Please fill in all required information')" />
    
    </form>
    
    										<script type="text/javascript">
    				document.getElementById('Username').onkeypress = validateKey;
    				function validateKey(evt) {
    				// get event and key
    				var evt = evt || window.event;
    				var key = String.fromCharCode(evt.charCode || evt.keyCode);
    				// check key
    if (key.match(/[0-9a-zA-Z\s]/)) {
    				return true;
    				}
    				return false;
    				}
    
    				</script>
    
    														<script type="text/javascript">
    								document.getElementById('FirstName').onkeypress = validateKey;
    								function validateKey(evt) {
    								// get event and key
    								var evt = evt || window.event;
    								var key = String.fromCharCode(evt.charCode || evt.keyCode);
    								// check key
    				if (key.match(/[a-zA-Z]/)) {
    								return true;
    								}
    								return false;
    								}
    
    				</script>
    
    														<script type="text/javascript">
    								document.getElementById('LastName').onkeypress = validateKey;
    								function validateKey(evt) {
    								// get event and key
    								var evt = evt || window.event;
    								var key = String.fromCharCode(evt.charCode || evt.keyCode);
    								// check key
    				if (key.match(/[a-zA-Z]/)) {
    								return true;
    								}
    								return false;
    								}
    
    				</script>
    
    																		<script type="text/javascript">
    												document.getElementById('State').onkeypress = validateKey;
    												function validateKey(evt) {
    												// get event and key
    												var evt = evt || window.event;
    												var key = String.fromCharCode(evt.charCode || evt.keyCode);
    												// check key
    								if (key.match(/[a-zA-Z\s]/)) {
    												return true;
    												}
    												return false;
    												}
    
    				</script>
    
    																		<script type="text/javascript">
    												document.getElementById('City').onkeypress = validateKey;
    												function validateKey(evt) {
    												// get event and key
    												var evt = evt || window.event;
    												var key = String.fromCharCode(evt.charCode || evt.keyCode);
    												// check key
    								if (key.match(/[a-zA-Z\s]/)) {
    												return true;
    												}
    												return false;
    												}
    
    				</script>
    
    				<script type="text/javascript">
    																document.getElementById('Year').onkeypress = validateKey;
    																function validateKey(evt) {
    																// get event and key
    																var evt = evt || window.event;
    																var key = String.fromCharCode(evt.charCode || evt.keyCode);
    																// check key
    												if (key.match(/[0-9]/)) {
    																return true;
    																}
    																return false;
    																}
    
    				</script>
    
    								<script type="text/javascript">
    																				document.getElementById('Email').onkeypress = validateKey;
    																				function validateKey(evt) {
    																				// get event and key
    																				var evt = evt || window.event;
    																				var key = String.fromCharCode(evt.charCode || evt.keyCode);
    																				// check key
    																if (key.match(/[a-zA-Z0-9@._]/)) {
    																				return true;
    																				}
    																				return false;
    																				}
    
    				</script>
    				<?php
    				include "lasttheme.php";
    ?>
    Animated Chatrooms - www.121chatrooms.net

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use the onchange event which will be fired when the user pastes into the control. You can then check to see whether there are any invalid characters using regex and either replace them or set the value to blank.

    i.e.

    Code xhtml:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Untitled Document</title>
            <link rel="stylesheet" type="text/css" media="screen" />
            <style type="text/css"></style>
            <script type="text/javascript">
                function validate(w) {
                    if(w.value.match('[^0-9 ]+')) {
                        alert('Invalid characters');
                        w.value = '';
                        return false;
                    }
                    return true;
                }
            </script>
        </head>
        <body>
     
            <input type="text" onchange="return validate(this);" />
     
        </body>
    </html>



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
  •