Requiere filling in text box before clicking on submit

JavaScript related questions should go here.
Post Reply
matthijs110
Posts: 10
Joined: Sat Jun 08, 2013 11:17 am

Requiere filling in text box before clicking on submit

Post 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
ScTech
Posts: 92
Joined: Sat Aug 24, 2013 8:40 pm

Re: Requiere filling in text box before clicking on submit

Post 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.
Last edited by ScTech on Sun Sep 15, 2013 3:45 pm, edited 1 time in total.
<?php while(!$succeed = try()); ?>
matthijs110
Posts: 10
Joined: Sat Jun 08, 2013 11:17 am

Re: Requiere filling in text box before clicking on submit

Post 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]
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Requiere filling in text box before clicking on submit

Post 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.
matthijs110
Posts: 10
Joined: Sat Jun 08, 2013 11:17 am

Re: Requiere filling in text box before clicking on submit

Post by matthijs110 »

Can you please give a example code on how to?
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Requiere filling in text box before clicking on submit

Post 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.
Post Reply