Form validation

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
davidjwest
Forum Commoner
Posts: 67
Joined: Sat Nov 06, 2004 5:26 am
Location: Leeds, Yorkshire, England

Form validation

Post by davidjwest »

I'm coding a game using PHP and need some form validation, I think javascript might be the way to go and I know almost nothing about that so wondered if anyone could help me please?

First of all, I want the submit button on form 1 to be grayed out until the user completes all the required fields.

Secondly, on form 2 I want the submit button grayed out until the total entered in four boxes equals 100. There are four boxes where the user can enter numbers and they must total 100. If it could also check for negative values that would be nice but not vital.

I will be using php to validate the input once it reaches the server just in case people have js turned off etc, but I think it would be nice to have the grayed out buttons to make it as easy as possible for the user.

Thanks very much!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

What do you need help with? Geying out the button until all fields complete?

Untested... but should get you started (or at least thinking in the direction).

Need more help re-post here ;-)

Code: Select all

<script type=&quote;text/javascript&quote;>
function checkForm(obj) {
    if (obj.field1 != &quote;&quote; && obj.field2 != &quote;&quote;) {
        obj.submit1.disabled = false;
    } else {
        obj.submit1.disabled = true;
    }
}
</script>
<form name=&quote;form1&quote; onKeyup=&quote;checkForm(this)&quote;>
Field 1: <input type=&quote;text&quote; name=&quote;field1&quote;>
<br />
Field 2: <input type=&quote;text&quote; name=&quote;field2&quote;>
<br />
<input type=&quote;submit&quote; name=&quote;submit1&quote; value=&quote;Submit!&quote; disabled>
</form>
Post Reply