Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Alert pop-up

Status
Not open for further replies.

DB2Problem

Programmer
Oct 17, 2002
53
US
Hi Javascript Gurus,

I have following problem and need help -

In my html page I have –

1. A Textbox
2. A Radio Button

I need to check if the radio button is checked, the textbox should mandatory have any interger value in it.

I think it can be done using Javascript, please help with the code, I will really appreciate your help

Thanks a lot..
 
DB2Problem,

Well, I'm new at this too. I can give you three functions that will do this in Internet Explorer. It will do all the popup boxes in Netscape, but doesn't cancel the event. That is a problem that I am currently working on.

It is long, but it will do what you asked for. Someone problably has a better way. I pasted them all together, and I just tested this, and it works.

First, you name your radiobuttons with IDs.

<input type=&quot;radio&quot; name=&quot;var1&quot; id=&quot;radiobutton1&quot; value=&quot;value1&quot;>
<input type=&quot;radio&quot; name=&quot;var1&quot; id=&quot;radiobutton2&quot; value=&quot;value2&quot;>
<input type=&quot;radio&quot; name=&quot;var1&quot; id=&quot;radiobutton3&quot; value=&quot;value3&quot;>
<input type=&quot;radio&quot; name=&quot;var1&quot; id=&quot;radiobutton4&quot; value=&quot;value4&quot;>

Second, name your text field.
<imput type=&quot;text&quot; name=&quot;var2&quot; id=&quot;textfield1&quot;>

In the submit button, add onclick=&quot;allFieldsChecked()&quot;

Here is the script to go in the header,


<script language=&quot;JavaScript&quot;>
function allFieldsChecked()
{
var radiochecked=&quot;false&quot;;
var radiobutton1=document.getElementById(&quot;radiobutton1&quot;).checked;
if (radiobutton1==true)
{
radiochecked=&quot;true&quot;;
}
var radiobutton2=document.getElementById(&quot;radiobutton2&quot;).checked;
if (radiobutton2==true)
{
radiochecked=&quot;true&quot;;
}
var radiobutton3=document.getElementById(&quot;radiobutton3&quot;).checked;
if (radiobutton3==true)
{
radiochecked=&quot;true&quot;;
}
var radiobutton4=document.getElementById(&quot;radiobutton4&quot;).checked;
if (radiobutton4==true)
{
radiochecked=&quot;true&quot;;
}
if (radiochecked==&quot;false&quot;)
{
alert(&quot;You have not selected a radiobutton&quot;);
}

//check texfield1
var textfield1=document.getElementById(&quot;textfield1&quot;).value;
if (textfield1==&quot;&quot;)

//if textfield1 is not filled out
{
alert(&quot;You have not filled out the text box.&quot;);
radiochecked=&quot;false&quot;;
}

//is textfield1 an integer?
else
{
radiochecked=IsNumeric(textfield1);
}

//if no radio button is checked, textfield1 not filled out, or textfield1 not an integer - cancel.
if (radiochecked==&quot;false&quot;)
{
window.event.returnValue = false;
}

}

function IsNumeric(strString)
{
var strValidChars = &quot;0123456789&quot;;
var strChar;
var blnResult = true;
if (strString.length == 0) return false;
for (i = 0; i < strString.length && blnResult == true; i++)
{
strChar = strString.charAt(i);
if (strValidChars.indexOf(strChar) == -1)
{
blnResult = false;
}
}
if (blnResult==false){MsgBox(&quot;The data you entered in not an integer. Please correct data.&quot;);}
return blnResult;
}

function MsgBox (textstring)
{
alert (textstring)
}
</script>
 
Thanks a lot, It worked and its much better..I don't expect more than this.. Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top