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.
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.
- The values the user enters
- A JS function that runs when the form is submits
But in the browser this could be changed by the user to something like:
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>
so you cannot use js validation to stop invalid inputs being inputted into a database or such.
Hope this helps!
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)