...

View Full Version : Javascript Help Needed with Alert Message..



coolguyraj
08-21-2007, 11:02 AM
Hi,

I have the following code.
<script>
function prob()
{
if ((document.getElementById("percentagego").value >100 )||(document.getElementById("percentagego").value ==0 ))
{
alert("% GO Must be between 1 and 100.");nbdb_update.percentagego.value = "";
}
if ((document.getElementById("percentagewin").value >100 )||(document.getElementById("percentagewin").value ==0 ))
{
alert("%WIN must be between 1 and 100.");nbdb_update.percentagewin.value = "";
}
if ((isNaN(parseInt(document.getElementById("percentagego").value)) || isNaN(parseInt(document.getElementById("percentagewin").value))))
{
document.getElementById("probability").value="";
}
else
{
var val1 = parseInt(document.getElementById("percentagego").value);
var val2 = parseInt(document.getElementById("percentagewin").value);
var ansd = document.getElementById("probability");
ansd.value = Math.round((val1 * val2)/100);
}
}
</script>

The problem here is the bothe the alert messages get displayed if the condition fails( i have marked the alert messages in bold)

How can i change the code to display only message relaveant to that text box?

Please help me.

Philip M
08-21-2007, 11:21 AM
Presumably you are saying that if both conditions fail both alerts are shown. You only want one.

function prob() {
var shown = 0;
if ((document.getElementById("percentagego").value >100 )||(document.getElementById("percentagego").value <1 ))
{
alert("% GO Must be between 1 and 100.");nbdb_update.percentagego.value = "";
shown = 1;
}

if (shown == 0) {
if ((document.getElementById("percentagewin").value >100 )||(document.getElementById("percentagewin").value <1 ))
{
alert("%WIN must be between 1 and 100.");nbdb_update.percentagewin.value = "";
}
}

coolguyraj
08-21-2007, 11:45 AM
This worked,

Thanks a lot.

coolguyraj
08-21-2007, 11:57 AM
Sorry to bother you again,

After i fill the first box with valid input and place the cursor on the net box i get the alert message for that box without any value in it.

How can i fix that?
function prob() {
var shown = 0;
if ((document.getElementById("percentagego").value >100 )||(document.getElementById("percentagego").value <1 ))
{
alert("% GO Must be between 1 and 100.");nbdb_update.percentagego.value = "";
shown = 1;
}

if (shown == 0) {
if ((document.getElementById("percentagewin").value >100 )||(document.getElementById("percentagewin").value <1 ))
{
alert("%WIN must be between 1 and 100.");nbdb_update.percentagewin.value = "";
}
}

Example: if i have text1=20 and text2='blank'
i get the alert "text 1 should be between 1 and 100"

i don want this ti happen.

please help.

Thanks,

coolguyraj
08-21-2007, 12:09 PM
Sorry to bother you again,

After i fill the first box with valid input and place the cursor on the net box i get the alert message for that box without any value in it.

How can i fix that?
function prob() {
var shown = 0;
if ((document.getElementById("percentagego").value >100 )||(document.getElementById("percentagego").value <1 ))
{
alert("% GO Must be between 1 and 100.");nbdb_update.percentagego.value = "";
shown = 1;
}

if (shown == 0) {
if ((document.getElementById("percentagewin").value >100 )||(document.getElementById("percentagewin").value <1 ))
{
alert("%WIN must be between 1 and 100.");nbdb_update.percentagewin.value = "";
}
}

Example: if i have text1=20 and text2='blank'
i get the alert "text 1 should be between 1 and 100"

i don want this ti happen.

please help.

Thanks,

amitthechosen1
08-21-2007, 12:09 PM
hi
Can you post the whole code along with the HTML part.

Thanks
Amit

coolguyraj
08-21-2007, 12:21 PM
<script>
function prob() {
var shown = 0;
if ((document.getElementById("percentagego").value >100 )||(document.getElementById("percentagego").value <1 ))
{
alert("% GO Must be between 1 and 100.");nbdb_update.percentagego.value = "";
shown = 1;
}

if (shown == 0) {
if ((document.getElementById("percentagewin").value >100 )||(document.getElementById("percentagewin").value <1 ))
{
alert("%WIN must be between 1 and 100.");nbdb_update.percentagewin.value = "";
}
}
if ((isNaN(parseInt(document.getElementById("percentagego").value)) || isNaN(parseInt(document.getElementById("percentagewin").value))))
{
document.getElementById("probability").value="";
}
else
{
var val1 = parseInt(document.getElementById("percentagego").value);
var val2 = parseInt(document.getElementById("percentagewin").value);
var ansd = document.getElementById("probability");
ansd.value = Math.round((val1 * val2)/100);
}
}
</script>

HTML part <tr>
<th class="bodyText" align="right">% Go:&nbsp;&nbsp;</th>
<td class="bodyText" colspan="2"><input type="text" id="percentagego" name="percentagego" onblur="prob()" maxlength="3" value="<?php
If($percentagego_update=="00"){
echo "100";
}else
echo $percentagego_update?>" size="17"></td>
</tr>
<tr>
<th class="bodyText" align="right">% Win:&nbsp;&nbsp;</th>
<td class="bodyText" colspan="2"><input type="text" id="percentagewin" name="percentagewin" onblur="prob()" maxlength="3" value="<?php
If($percentagewin_update=="00"){
echo "100";
}else
echo $percentagewin_update;?>"size="17"></td>
</tr>


<tr>
<th class="bodyText" align="right">Probability:&nbsp;&nbsp;</th>
<td class="bodyText" colspan="2"><input type="text" readonly="readonly" onblur="prob()" name="probability" id="probability" value="<?php
if (!empty($probability_update)) {
echo number_format($probability_update,0);
}?>" maxlength="3" size="5"></td>
</tr>

amitthechosen1
08-21-2007, 12:34 PM
Hi Raj,
The problem is because you are calling the function using the onBlur functionality of Javascript.
You need to change that to onChange.
Please find below, a sample code that is designed using the basic 2text boxes, and it is working fine.

You can directly paste it in the HTML and run.

</html>
<script>
function prob() {
alert("getting called");
var shown = 0;
if ((document.getElementById("percentagego").value >100 )||(document.getElementById("percentagego").value <1 ))
{
alert("% GO Must be between 1 and 100.");document.forms[0].percentagego.value = "";
shown = 1;
}

if (shown == 0) {
if ((document.getElementById("percentagewin").value >100 )||(document.getElementById("percentagewin").value <1 ))
{
alert("%WIN must be between 1 and 100.");
document.forms[0].percentagewin.value = "";
}
}
}
</script>


<form>
<input type="percentagego" name="percentagego" onChange="prob()">
<input type="percentagewin" name="percentagewin" onChange="prob()">

</form>


I Hope your problem will be solved on doing these changes.

Regards
Amit.

coolguyraj
08-21-2007, 01:07 PM
Hi Amit,

Thanks for the solution.

I still have the problem the code works fine but

Here is the problem.

1. i have two text boxes blank.
2 i fill the first text box with valid input and move to the second text box(now i get the alert message saying the "text2 is invalid) I Dont want this to appear unless the data is feed to the box.

Thanks,
RAJ

amitthechosen1
08-21-2007, 01:40 PM
Hi Raj
can you confirm that you are using onChange in all the Input text fields.
if you are doing that, then problem should not occur.

Else please post the code once again, and how to run that.

Regards
Amit

coolguyraj
08-21-2007, 01:48 PM
Hi Amit,

Here is the code.

My main aim is not to display anything related to the second text box untill i fill something it (May be an invalid value,in our case number >100)


<script>
function prob()
{
document.getElementById("probability").value = "";
var percgo = parseInt(document.getElementById("percentagego").value);
if(isNaN(percgo) || percgo < 1 || percgo > 100) {
alert("% GO Must be a Number and Between 1 and 100.");
nbdb_update.percentagego.value = "";
return;
}
document.getElementById("probability").value = "";
var percwin = parseInt(document.getElementById("percentagewin").value);
if(isNaN(percwin) || percwin < 1 || percwin > 100) {
alert("% WIN Must be a Number and Between 1 and 100.");
nbdb_update.percentagewin.value = "";
return;
}

var ansd = document.getElementById("probability");
ansd.value = Math.round((percgo * percwin)/100);
}
</script>


HTML CODE
<tr>
<th class="bodyText" align="right">% Go:&nbsp;&nbsp;</th>
<td class="bodyText" colspan="2"><input type="text" id="percentagego" name="percentagego" onchange="prob()" maxlength="3" value="<?php
If($percentagego_update=="00"){
echo "100";
}else
echo $percentagego_update?>" size="17"></td>
</tr>
<tr>
<th class="bodyText" align="right">% Win:&nbsp;&nbsp;</th>
<td class="bodyText" colspan="2"><input type="text" id="percentagewin" name="percentagewin" onchange="prob()" maxlength="3" value="<?php
If($percentagewin_update=="00"){
echo "100";
}else
echo $percentagewin_update;?>"size="17"></td>
</tr>


<tr>
<th class="bodyText" align="right">Probability:&nbsp;&nbsp;</th>
<td class="bodyText" colspan="2"><input type="text" readonly="readonly" onblur="prob()" name="probability" id="probability" value="<?php
if (!empty($probability_update)) {
echo number_format($probability_update,0);
}?>" maxlength="3" size="5"></td>
</tr>


Basically i am trying to take input from two text boxes and calculating the result and displaying it in the third column.

Thanks,
RAJ

Philip M
08-21-2007, 04:18 PM
<td class="bodyText" colspan="2"><input type="text" readonly="readonly" onblur="prob()" name="probability"

amitthechosen1 has pointed out what is wrong.

amitthechosen1
08-22-2007, 07:05 AM
Hi Raj,
Hope the problem is solved.
I dont have a set up for PHP else would have simulated the whole code and solved.
If u can provide with what i need to do to compile, the I may be able to help.

Regards,
Amit

coolguyraj
08-22-2007, 10:13 AM
Thanks Philip and Amit for all the help.

I have fixed it.This is what i have done.function prob()
{
document.getElementById("probability").value = "";
var percgo = parseInt(document.getElementById("percentagego").value);
if(isNaN(percgo) || percgo < 1 || percgo > 100) {
alert("% GO Must be a Number and Between 1 and 100.");
nbdb_update.percentagego.value = "";
return;
}
if(document.getElementById("percentagewin").value == "") {
return;
}
var percwin = parseInt(document.getElementById("percentagewin").value);
if(isNaN(percwin) || percwin < 1 || percwin > 100) {
alert("% WIN Must be a Number and Between 1 and 100.");
nbdb_update.percentagewin.value = "";
return;
}

var ansd = document.getElementById("probability");
ansd.value = Math.round((percgo * percwin)/100);
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum