Results 1 to 3 of 3

Thread: Form Validation

  1. #1
    Join Date
    Apr 2014

    Form Validation

    Hey guys,

    So I'm creating a user register form for my website and I have a question about it.

    When I'm using JS to validate my form, where does that go exactly? Does it go to the action file that the html file containing the register form designates? Does it go in the header of the html file that contains the form?

    I'm trying to get the effect where you submit the register form and you get an alert() if you've done something incorrectly. And like I said, I'm just trying to figure out where the JS form validation code goes.


  2. #2
    Join Date
    Dec 2012
    You can put it in a .js file and link to it from the html page that contains the form. If you want to validate without leaving the page it will need to be on the page that has the form.

    You can find a very simple example here with the validation code just in the head tag.


  3. #3
    Join Date
    Jul 2013
    Put simply, JavaScript forum validation can be run by making the form not submit if there was an error in the users input.

    It would also be good to mention that JavaScript (being client side) cannot be used to stop a user trying to hack a site or any other malicious code. JavaScript can only be used to validation a user who is trying to use your site with good intent because JS can be altered (unlike something like php which runs server side)

    So keeping that in mind, to use javascript validation you will need:
    • The values the user enters
    • A JS function that runs when the form is submits

      function Validation(Values) // Array of Values to check //
        for(var i=0;i<Values.length;i++) // for each value in array //
    	  var elm = document.getElementById(Values[i]) // DOM element with the id of the array item (ie, id='PassWord')
    	  if(elm.value == '') // if it does not have a value set//
    	    alert('Please Enter A ' + Values[i]);
    		return false; // will tell the onsubmit to return false and alert the first error//
    <form onsubmit="return Validation(['UserName','PassWord'])" action='LoginPage.html' method="POST">
      <dd><input type="text" name="us" id="UserName"/></dd>
      <dd><input type="text" name="us" id="PassWord"/></dd>
      <button  type="submit" id="sb">Submit</button>
    But in the browser this could be changed by the user to something like:
    onsubmit="return true"
    so you cannot use js validation to stop invalid inputs being inputted into a database or such.

    Hope this helps!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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