Connecting Tech Pros Worldwide Help | Site Map

enable submit when all fields are popuplated

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 07:50 PM
Mel
Guest
 
Posts: n/a
Default enable submit when all fields are popuplated

i want to diable all submit_buttons untill all my form elements are
populated.

how can i do that ?
thanks



  #2  
Old July 23rd, 2005, 07:51 PM
RobG
Guest
 
Posts: n/a
Default Re: enable submit when all fields are popuplated

Mel wrote:[color=blue]
> i want to diable all submit_buttons untill all my form elements are
> populated.
>
> how can i do that ?
> thanks
>
>[/color]

Have an onload function that sets the submit button's disabled
attribute to true.

<body onload="document.forms['formA'].elements['sB'].disabled=true;">

Put an onblur or onfocus attribute on all your form elements that
calls a script to validates the form (you may want to attach this using
JavaScript when you disable the submit button to save code). When the
form passes validation, set the submit button's disabled attribute to
false.

The script below puts an onblur function on the text and textarea
elements, as well as adding an onsubmit function on the form. It
disables the submit button, and only re-enables it if the form passes
validation. The form is validated again when submitted.

Note that you should also validate at the server, do not trust that the
client-side script has actually done the job or that the user has not
subverted it.


<head>

...

<script type="text/javascript">
function initForm(f){
if ( f = document.forms[f] ) {
f.onsubmit = function() {return checkForm(this)};
} else {
return;
}
var el, els = f.elements;
for (var i=0, j=els.length; i<j; i++) {
el = els[i];
if ( 'text' == el.type ||
'TEXTAREA' == el.nodeName ){
el.onfocus = function() {checkForm(this.form)};
} else if ('submit' == el.type) {
el.disabled = true;
}
}
}

function checkForm(f){
var el, els = f.elements, i=els.length;
while (i--) {
el = els[i];

// Do vaildation on 'el'. If no good, return false
// If passes validation, allow script to continue

if ('submit' == el.type) {
el.disabled = false;
return; // Assuming there is only one submit button
}
}
}
</script>
</head
<body onload="initForm('formA');">
<form action="" name="formA">
<textarea></textarea>
<input type="text" size="10">
<input type="submit">
</form>
</body>




Realise that anyone with JavaScript disabled will be able to submit
your form anyway. If you make the submit button disabled using the
HTML source and enable it using JavaScript as suggested above, users
with JavaScript disabled will not be able to submit your form at all.




--
Rob
  #3  
Old July 23rd, 2005, 07:51 PM
RobG
Guest
 
Posts: n/a
Default Re: enable submit when all fields are popuplated

RobG wrote:
[...][color=blue]
> if ( 'text' == el.type ||
> 'TEXTAREA' == el.nodeName ){
> el.onfocus = function() {checkForm(this.form)};[/color]

Agggh, that should be onblur of course.

el.onblur = function() {checkForm(this.form)};

[color=blue]
> } else if ('submit' == el.type) {
> el.disabled = true;
> }[/color]
[...]


--
Rob
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.