View Full Version : JavaScript Advice
Sammie
October 18th, 2007, 07:19
I am starting to lean JavaScript and am working through a tutorial my teach has give me and am currently stumped on what i am trying to do.
What i am doing is have a form with an inputbox which you can enter a number between 0 and 10 and press send although if it's over 10 it should bring up an Alert box which then tells the use a message.
The following is my code, i'm totally not sure of what i am doing wrong.
<html>
<head>
<title>New JavaScript page </title>
<script type="text/javascript">
function ten() {
var number = (form.textbox.value);
if (number > 10) {
alert("You have entered an incorrect number, please try again!")
}
}
</script>
</head>
<body>
<form name="form">
Enter a number less than or equal to 10
<input type="text" name="textbox">
<input type="button" value="Go >" onclick="ten">
</form>
</body>
</html>
krakjoe
October 18th, 2007, 08:16
<html>
<head>
<script language="javascript">
function ten( )
{
if( document.theForm.theNumber.value )
{
if( document.theForm.theNumber.value > 10 )
{
alert( "You have entered an incorrect number, please try again." );
}
else if( document.theForm.theNumber.value <= 10 )
{
alert( "You have entered an acceptable number" );
}
else
{
alert( "You have not entered a number" );
}
}
}
</script>
</head>
<body>
<form action="" method="post" name="theForm">
<input type="text" name="theNumber" />
<input type="button" onclick="ten( )" value="Test"/>
</form>
</body>
</html>
Sammie
October 20th, 2007, 03:56
Thanks ever so much for the help their, it did work which si awesome!
I have actually run into another problem with another section from the tutorial, the code i have is as following.
<html>
<head>
<script language = "JavaScript">
function CheckEmail() {
var EmailAddress = (Form1.Textbox1.value);
var Length = EmailAddress.length;
var Count = 0;
var Loop = 0;
var Character
while (Loop < Length)
Character = EmailAddress.charAt(Loop);
if (Character == "@") {
Count = Count + 1;
}
Loop = Loop + 1;
}
if (Count != 1) {
alert("Please check your email address");
Form1.Textbox1.select();
}
else {
alert("Your email address has one '@' symbole")
}
}
</script>
</head>
<body>
<form name="Form1">
Enter your email address<br />
<input type="text" name="Textbox1">
<input type="button" value="Test" onclick="CheckEmail()">
</form
</body>
</html>
And here is an image of what it SHOULD be doing but nothing will happen
http://img526.imageshack.us/img526/3407/screen11qm1.png
Any help would be appreciated, i don't think i have done anything wrong, if it is something wrong with the tutorial i'll have to tell my teacher to scrap it.
krakjoe
October 20th, 2007, 04:29
Whoever is writing these is donig an appauling job of it ...
<html>
<head>
<script language = "JavaScript">
function CheckEmail() // Declare CheckEmail function
{
var Address = document.Form1.Textbox1.value ; // Retrieve Email address from form
var Count = 0 ; // Setup a counter for @ symbols
if( !Address.length ) // Check the email has characters in
{
alert( "Please enter an email address" ); // Invalid email address
return ; // Return control to the webpage
}
for( var i = 0; i < Address.length; i++ ) // Loop over the string
{
if( Address.charAt( i ) == '@' ) // And count the @ symbols
{
Count++;
}
}
if( Count ) // If we have @ symbols
{
alert( "Your email address has " + Count + " @'s in" ); // Tell the user how many
}
else // No @ symbols.
{
alert( "Your email address is invalid" );
}
}
</script>
</head>
<body>
<form name="Form1">
Enter your email address<br />
<input type="text" name="Textbox1">
<input type="button" value="Test" onclick="CheckEmail()">
</form
</body>
Sammie
October 20th, 2007, 05:47
The tutorial says they are by orbeducation.com, it's really strange that the codes they are saying are not working, you'd do a better job writing one, thanks for all your help btw, i'll be telling my teacher to scrap the tutorial.
If you don't mind can you help me on another thing regarding strings, i have the following
<html>
<head>
<script type="text/javascript">
var string = prompt("Please provid a word with more than 4 letters");
var set1 = string.length;
alert(set1)
</script>
</head>
<body>
</body>
</html>
What i am wanting to is print is the last 3 letters out from the word entered in the prompt box, i understand you have to use substr(a,b) but i am not sure how i go around editing my coding to do what i want.
Cheers
Sam
krakjoe
October 20th, 2007, 05:58
<html>
<head>
<script type="text/javascript">
var string = prompt("Please provid a word with more than 4 letters");
alert( string.substr( string.length - 3 ) );
</script>
</head>
<body>
</body>
</html>
Sammie
October 27th, 2007, 02:14
Thanks so much for your help, much appreciated.
I am starting on a new task where their are fields which you have to fill in before the form is redirected to the next page, JavaScript form validation. the following is what i have.
<html>
<head>
<script type="text/javascript">
function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
{alert(alerttxt);return false}
else {return true}
}
}
function form_validate(thisform)
{
with (thisform)
{
if (validate_required(fname,"Your first name must be filled out!")==false)
{fname.focus();return false}
}
}
</script>
</head>
<body>
<form action="sigupcomplete.php"
onsubmit="return form_validate(this)"
method="post">
First name: <input type="text" name="fname"><br />
Last name: <input type="text" name="lname"><br />
Email: <input type="text" name="mail">
<input type="submit" value="Submit">
</form>
</form>
</body>
</html>
Now how would i go about creating a new part in the JavaScript section for the last name field? and also how would i intergrade the section for the email field with having a required @ and at leats 1 dot
Cheers
Sam
Decker
October 27th, 2007, 07:44
How much are you charging this guy Joe? :lol:
You've got one hell of a tutor now Sammie :)
Sammie
October 28th, 2007, 07:11
Just an update, disregard my post up above as i have worked it out and is working fine thus far, although how would i go around making a new function for a number field which will only accept numbers?
Cheers
Sam
krakjoe
October 28th, 2007, 11:55
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script language="javascript">
function numbersOnly( element )
{
if( !element.value.match( /^([0-9]+)$/ ) )
{
element.value = element.value.substr( 0, element.value.length - 1 ) ;
alert( "Numbers only please" );
}
}
</script>
<title>Untitled Document</title>
</head>
<body>
<input type="text" value="" name="thename" onkeyup="numbersOnly( this )" />
</body>
</html>
something like that ...
Powered by vBulletin® Version 4.1.7 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.