Click to See Complete Forum and Search --> : Simple validation problem


Ash
08-21-2003, 11:23 AM
Hi,

I've got a really simple checkform that checks that a message field is not empty, then asks the user if they want to submit the message from XXXX, where XXXX is defined by the following:


You: <input type="radio" name="author" value="<?=$current_username?>" CHECKED>

Them: <input type="radio" name="author" value="<?=$their_name?>">

Comment: <input type="radio" name="author" value="<?=$current_username?>">


3 radios that change the value of 'author' depending on which is selected.

Here's the validate:


<script language="JavaScript">
function checkform (form) {
if (form.message.value == "") {
alert( "Please enter the communication with the customer." );
form.message.focus();
return false ;
}
else {
var agree=confirm("Are you sure you wish to submit this information from "+form.author.value+"?");
if (agree) return true ;
else return false ;
}
}
</script>


The 'non empty' validation works OK, but the only message I can get out is "Are you sure you wish to submit this information from Undefined?" - not what I had in mind.

I'm sure it's a simple problem, but anyone got any ideas ???

Thanks,
Ash

learninghtml
08-21-2003, 11:18 PM
Hi,
Try changing "form.message.value" to "form.message.text"
and see what that does.

I think it's saying "undefined" because if it's a text box it doesn't understand "value" as a property of "message". Maybe but not sure, easy enough to try though.

Hope that helps...

LearningHTML

Ash
08-26-2003, 03:18 AM
heh-heh, I've posted the wrong code !

I've changed the last part of that function to be

[quote]
var agree=confirm("Are you sure you wish to submit this information from "+form.author.value+"?");
if (agree) return true ;
else return false ;
[quote]

So it should take the value of 'author' (either $current_username or $their_name) and display it at the end of the 'confirm' string.

Is this the right way to get the value of a radio button, or is there a better way?

Any help most appreciated.
Ash

Ash
08-26-2003, 10:22 AM
OK, I've found a way ...

If you add an ID into the radio button:

You: <input type="radio" name="author" id="r1" value="<?=$current_username?>">

Them: <input type="radio" name="author" id="r1" value="<?=$their_name?>">

Comment: <input type="radio" name="author" id="r1" value="<?=$current_username?>">

and then you use:

if(document.getElementById('r1').checked == true) {
var agree=confirm("Are you sure you wish to email this information to the customer?");
if (agree) return true ;
else return false ;
}
if(document.getElementById('r2').checked == true) {
var agree=confirm("Submit this information from the customer?");
if (agree) return true ;
else return false ;
}
if(document.getElementById('r3').checked == true) {
var agree=confirm("Are you sure you wish to submit this comment?");
if (agree) return true ;
else return false ;
}

It might not be very graceful, but it works a treat!