Page 1 of 1

Requiere filling in text box before clicking on submit

Posted: Sun Sep 15, 2013 8:12 am
by matthijs110
Hello,

Is it possible to add a javascript coding into HTML? If so, is it possible to add a code that requires to fill in a text box before you can click on the submit button? So.. If i don't fill in the text box, then i can't click the button.

Matt

Re: Requiere filling in text box before clicking on submit

Posted: Sun Sep 15, 2013 3:40 pm
by ScTech
It depends on your HTML markup. Also, don't just fall back on JavaScript because it can be disabled. If in your markup you have a name on your text box, it's pretty straight forward.
[syntax=javascript]if(document.getElementsByName("your_name").value == "") {
// Empty
}[/syntax]

If you want to make sure they just don't add a space, well that's a lot trickier because of IE and it's compatibility issues. Alternatively you can use a regex to put stricter standards on the input. You could say, require only numbers and letters.

Re: Requiere filling in text box before clicking on submit

Posted: Sun Sep 15, 2013 3:45 pm
by matthijs110
Well, this is my button:

[syntax=xhtml]<button class="btn btn-info" id="buy" value="Buy" name="submit">Buy VIP</button>[/syntax]

And this is my text box:
[syntax=xhtml]<input name="custom" type="text" id="custom" size="45" placeholder="Minecraft username.." required>[/syntax]

Re: Requiere filling in text box before clicking on submit

Posted: Mon Sep 16, 2013 1:08 am
by Temor
Well, yes. You'd have to add an onclick event on your button that triggers the code ScTech posted.
You should check if it's not empty, and then if it filled, you send the post data to where you want it.

There are plenty of examples of this in practice online.

Re: Requiere filling in text box before clicking on submit

Posted: Mon Sep 16, 2013 4:18 am
by matthijs110
Can you please give a example code on how to?

Re: Requiere filling in text box before clicking on submit

Posted: Mon Sep 16, 2013 4:39 am
by Temor
matthijs110 wrote:Can you please give a example code on how to?


I haven't actually watched this video, so I don't know if it's any good. But there are only so many ways you can validate forms using javascript.