943,917 Members | Top Members by Rank

Ad:
Jun 26th, 2008
0

Validate dropdown and some other

Expand Post »
Hi all again

I'm very very new to javascript, post a help here on validate radio button got solve with the alert msg but still waiting for the solution on selecting either M or F.

My next problem is validate dropdown menu, my default is select, i want the user to select a rating from 1 to 10 before they can submit. tried alot of matter but it still fail me so can any wan add in the function for me thk's.

And im ask to send the data to my email after they submit but i hav no ideal no to go about , can show me how's it done ??

<html>
<head><title> Kelvin's Guestbook </title>
<style>
H1, H2, H3, H4, H5, H6 {color:#D4A017; text-align:center}
</style>
<script language="Javascript" type="text/javascript">

function validate_length(field)
{
with(field)
{
if (value.length == 0)
{
alert("Pls Enter Your Name!"); return false;
}else return true;

}
}


function validate_email(field)
{
with (field)
{
apos=value.indexOf("@");
dotpos=value.lastIndexOf(".");
if (apos<1||dotpos-apos<2)
{
alert("Not a valid e-mail address!"); return false;
}else return true;
}
}


function validate_gender(gM, gF)
{

if (gM.checked)
{
return true;
}
if (gF.checked)
{
return true;
}

alert("Pls Select Your Gender");
return false;


}


function validate_form(thisform)
{
with (thisform)
{
if (validate_length(yourname) == false)
{
yourname.focus(); return false;
}
if (validate_email(email)==false || validate_email(email)==false)
{
email.focus(); return false;
}
if (validate_gender(genderM, genderF) == false)
{
genderM.focus(); return false;
}

}
}

function startclock()
{
var thetime=new Date();

var nhours=thetime.getHours();
var nmins=thetime.getMinutes();
var nsecn=thetime.getSeconds();
var nday=thetime.getDay();
var nmonth=thetime.getMonth();
var ntoday=thetime.getDate();
var nyear=thetime.getYear();
var AorP=" ";

if (nhours>=12)
AorP="P.M.";
else
AorP="A.M.";

if (nhours>=13)
nhours-=12;

if (nhours==0)
nhours=12;

if (nsecn<10)
nsecn="0"+nsecn;

if (nmins<10)
nmins="0"+nmins;

if (nday==0)
nday="Sunday";
if (nday==1)
nday="Monday";
if (nday==2)
nday="Tuesday";
if (nday==3)
nday="Wednesday";
if (nday==4)
nday="Thursday";
if (nday==5)
nday="Friday";
if (nday==6)
nday="Saturday";

nmonth+=1;

if (nyear<=99)
nyear= "19"+nyear;

if ((nyear>99) && (nyear<2000))
nyear+=1900;

document.clockform.clockspot.value
=nhours+": "+nmins+": "+nsecn+" "+AorP+" "+nmonth+"/"+ntoday+"/"+nyear+","+nday;


setTimeout('startclock()',1000);

}

</script>
</head>
<body>

<h1>Welcome to my GuestBook</h1>

<CENTER>
<IMG SRC="Icon 1.gif" WIDTH="1000px">
</CENTER>

<FORM name="clockform">
<p align="right">
Current Time:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<br><INPUT TYPE="text" name="clockspot" size="32">
</p>
</FORM>
<SCRIPT language="JavaScript">
startclock();
</SCRIPT>


<Center>

<form name="guestbook" action="1.html"onsubmit="return validate_form(this);"
method="post">

<br>Name : <input type="text" name="yourname" value="">
<br>Email: <input type="text" name="email" size="30">
<br>Gender: <input type="radio" name="genderM" value="Male" id="Male">Male
<input type="radio" name="genderF" value="Female" id="Female">Female
<br>My Website Rating:
<select name="Number">
<option value="selected">SELECT</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<br>Comments: *Optional
<br><textarea cols="50" rows="8" name="Coments"></textarea>
<br><input type="submit" name="submit" value="Sign GuestBook">
<input type="Reset" name="Reset" value="Reset">
</form>
</Center>
</body>
</html>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
darkraider is offline Offline
8 posts
since Jun 2008
Jun 27th, 2008
0

Re: Validate dropdown and some other

For the radio button u need to group both radio buttons that means u gona have the same name for both of them u can change this line of code

<input type="radio" name="genderM" value="Male" id="Male">Male
<input type="radio" name="genderF" value="Female" id="Female">Female

to this

<input type="radio" name="optGender" value="Male" id="Male">Male
<input type="radio" name="optGender" value="Female" id="Female">Female

and then again u need to change ur script to this
function validate_gender()
{
var choice = false;

for(var x = 0; x < document.theNameOftheForm.optGender.length; x++)
{
	if(document.theNameofTheform.optGender[x].checked == true)
	choice = true;
}else{
	alert("Pls Select Your Gender");
}
Last edited by Traicey; Jun 27th, 2008 at 5:43 am.
Reputation Points: 26
Solved Threads: 19
Posting Whiz in Training
Traicey is offline Offline
283 posts
since Mar 2008
Jun 27th, 2008
0

Re: Validate dropdown and some other

Oh I forgot...... for drop down list you need to change
this line of code
<option value="selected">SELECT</option> to this
<option value="0"SELECTED>SELECT</option>
And to validate would be something like this
function validateRating(form)
{
	var Rating = 1;

	if (form.Number.selectedIndex == 0) 
                {
		alert("Please select a rating");
		Rating = 0;
	}

	if (Rating) {
		form.submit();
	}
}
Last edited by Traicey; Jun 27th, 2008 at 5:45 am.
Reputation Points: 26
Solved Threads: 19
Posting Whiz in Training
Traicey is offline Offline
283 posts
since Mar 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: change values of repeat region textfield
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Floting menu for Browser compatibility





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC