[HTML/JavaScript] Popup when submit with check
Heya!
I'm busy scripting!
Yet I have a problem...
Making a form is easy :) Making it submit in a Database, is to... (it's a register form)
But I need something that checks if everything is filled in and then if not gives a popup what is missing and if everything is right, it will just say something else.
Can someone help me with a short script that works with one form, I can edit it myself (I think) to make it work for the rest.
Thnx in advance
Lex! :w00t:
Re: [HTML/JavaScript] Popup when submit with check
on submit something like
Code:
function check() {
var obj = document.getElementByID("username_field");
if(obj.value == "") {
alert("Please fill in all fields");
}
}
Re: [HTML/JavaScript] Popup when submit with check
And username_field is the name of the input of the username?
Re: [HTML/JavaScript] Popup when submit with check
Re: [HTML/JavaScript] Popup when submit with check
Quote:
Originally Posted by
Lexil123
And username_field is the name of the input of the username?
Actually the ID.. Hence the function "getElementById"
That's an odd way to do form submission, though. One of the less disgusting parts of the DOM is form handling.
HTML DOM Form Object
You can get all of the form elements using form.elements.
You can use the form's submit event to catch any submissions by particular forms. From there you can execute code or stop the submission from occurring altogether.
Here's an example of getting values from a form,
Edit this Fiddle - jsFiddle
(Note that console.log will send output to the console.. which you may be able to right-click -> inspect element to see the result on Chrome or Opera or if you have FireBug for Firefox. You may think the form does nothing, but it logs all of the names and values to the console- for educational purposes.) Look at the 'object' in the console.
Code:
values['message'] //Hello, World! - value of: <textarea name="message">
The example is slightly changed from w3schools to be more... right.
Note that I have a tag:
<p value="foobar">
Which shows you that only form elements are used in the "elements" object - though you cannot guarantee all browsers will implement the JavaScript language the same. You can feel pretty safe that 'p' tags won't end up in your 'form' elements unless your browser is using a language that is not JavaScript.
Re: [HTML/JavaScript] Popup when submit with check
Will check it out, thnx :) will get back to you if I have any problems
EDIT: For some reason it wont work... added the ID, but doesnt seem to find the value
EDIT2: Got it! THNX :D