...

View Full Version : 2 forms which has 3 input boxes per each in alerting



joonstar
05-28-2005, 06:30 AM
<script type = 'text/javascript'>

function validate(f) {

if (f['title1'].value.length == 0) {
alert('Title1, please');
return false
}

if (f['message1'].value.length == 0) {
alert('Message1, please');
return false
}

if (f['password1'].value.length == 0) {
alert('password1, please');
return false
}

return true
}
</script>

<form action='action.cfm' method='post' onsubmit='return validate(this)'>

<input name='title1' size='70'><br>

<textarea name='message1'
cols='70'
rows='10'></textarea><br>

<input name='password1' size='70'><br>

<input type='submit' value='submit'>

</form>


The above code has one form and it works fine in alert.
I like to make it work in 2 forms. so I made the following code.




<script type = 'text/javascript'>

function validate(f) {

if (f['title1'].value.length == 0) {
alert('Title1, please');
return false
}

if (f['message1'].value.length == 0) {
alert('Message1, please');
return false
}

if (f['password1'].value.length == 0) {
alert('password1, please');
return false
}

return true
}
</script>

<form action='action.cfm' method='post' onsubmit='return validate(this)'>

<input name='title1' size='70'><br>

<textarea name='message1'
cols='70'
rows='10'></textarea><br>

<input name='password1' size='70'><br>

<input type='submit' value='submit'>

</form>


<script type = 'text/javascript'>

function validate(f) {

if (f['title2'].value.length == 0) {
alert('Title2, please');
return false
}

if (f['message2'].value.length == 0) {
alert('Message2, please');
return false
}

if (f['password2'].value.length == 0) {
alert('password2, please');
return false
}

return true
}

</script>


<form action='action.cfm' method='post' onsubmit='return validate(this)'>

<input name='title2' size='70'><br>

<textarea name='message2'
cols='70'
rows='10'></textarea><br>

<input name='password2' size='70'><br>

<input type='submit' value='submit'>

</form>

The above trial code which has two javascripts and 2 forms doesn't work correctly.

The second form works fine in alerting but the first form doesn't work correctly in alerting?

How can I make it work both the first form and the second form in alerting?

Philip M
05-28-2005, 08:50 AM
You have two functions with the same name validate().

I suggest you rename them validate1() and validate2().

Hence :- onsubmit='return validate1(this)'

glenngv
05-30-2005, 12:44 PM
I suggest you rename them validate1() and validate2().
But you can make a generic function that works for both form.


function validate(f, suffix) {
if (f['title'+suffix].value.length == 0) {
alert('Title'+suffix+', please');
return false;
}
if (f['message'+suffix].value.length == 0) {
alert('Message'+suffix+', please');
return false;
}
if (f['password'+suffix].value.length == 0) {
alert('Password'+suffix+', please');
return false;
}
return true;
}
...
<form action="action.cfm" method="post" onsubmit="return validate(this, 1)">
...
<form action="action.cfm" method="post" onsubmit="return validate(this, 2)">



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum