javascript - Check $_POST data with jquery before submitting the form -


i have form sends store first name of user in database. checking information send user using regex in php. make project more interactive, decided validate information jquery before sending php.

my project looks this:

<script type="text/javascript" src="jquery-2.2.3.min.js"></script>  <script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js" type="text/javascript"></script>    <body>      <form >          <div>              <label>first name</label>              <input name="firstname" type="text">          </div>          <div>              <input type="submit">          </div>      </form>  </body>  </html>    <script>      $(document).ready(function() {                    $("form").submit(function (e) {                            var firstname = $(this).find('input[name="firstname"]').val();                var regex = /^[a-za-z0-9 \-']+$/;//only numbers, letters, dashes, apostrophes , spaces accepted              if(regex.test(firstname)){                  alert('valid name.');              }else{                  alert('invalid name.');                  e.preventdefault();              }          });      });  </script>

now have 2 questions:

  1. is need check first name in php again before storing data in database ? (to improve security)
  2. how can submit form right after alert('valid name.'); ?

thanks providing help.

first of have in mind validation of users input implementing @ server side of application only!!! can not validate input data @ client side js because can passed easy(either disabling javascript or using tools curl).

however can increase user experience validate input before submitting form or inform user forgot fill in input.

to inform user not fill in input can use new html 5 attribute required above

username: <input type="text" name="usrname" required> 

the required attribute not let user submit form unless had filled associated input.

also can use maxlength attribute address use case "a password must have x max letters.

password: <input type="password" name="pass" maxlength="8" size="8"><br> 

how validate input @ server side

there many techniques , can find of them here @ stackoverflow. Ι refer top voted post how can prevent sql injection in php? answer question.

just 2 bullets compact above post suggest read otherwise

  • always escape data

  • use mysqli instead of mysql

how can submit form right after alert('valid name.'); ?

this easy use code

    <form action="action_page.php" method="post">     <div>         <label>first name</label>         <input name="firstname" type="text">     </div>     <div>         <input type="submit">     </div> </form> 

the above code "send" user's input process @ action_page.php using post method, can read using $_post supergloba table $firstname = $_post['fistsname'] etc.


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -