 |
| Javascript How-To Ask your "How do I do this with Javascript?" questions here. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

March 7th, 2004, 07:38 AM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
check if empty
when the user clicked the submit button, it will check if there is any value for the telephone textbox.If no value, then alert the user and stop submitting else submit the form.But my code below has does not work. Please help me correct it.
<html>
<head>
<title>Testing</title>
<style>
#textbox {position:absolute; left:200px};
</style>
<script>
function check(text)
{
if(!text.value))
{
alert("Please enter telephone number");
text.select();
text.focus();
return false;
}
}
</script>
</head>
<body onload="document.myform.telephone.select(); document.myform.telephone.focus();">
<form name="myform" >
Telephone No. <input id="textbox" type="text" name="telephone" ><br><br>
<input type="button" value="Submit" onclick="check(document.myform.telephone.value)"> <br><br>
</form>
</body>
</html>
|
|

March 7th, 2004, 09:04 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
if(!text.value))
A) The syntax is wrong. It should be:
if(!text.value)
B) This test will only tell you if there is a valid "value" property. There will always be a valid value property, but it might be blank. So you must test accordingly:
if(text.value.length()==0)
You may also want to look into testing this field with a phone number regular expression.
Peter
------------------------------------------------------
Work smarter, not harder.
|
|

March 7th, 2004, 09:07 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
A few changes needed.
Code:
<html>
<head>
<title>Testing</title>
<style>
#textbox {position:absolute; left:200px};
</style>
<script>
function check(textbox)
{
if(textbox.value == "")
{
alert("Please enter telephone number");
//textbox.select();
text.focus();
//return false;
}
textbox.form.submit();
}
</script>
</head>
<body onload="document.myform.telephone.select(); document.myform.telephone.focus();">
<form name="myform" >
Telephone No. <input id="textbox" type="text" name="telephone" ><br><br>
<input type="button" value="Submit" onclick="check(document.myform.telephone)"> <br><br>
</form>
</body>
</html>
As your button is not a submit button you'll need to submit manually. I commented out the lines that didn't add anything.
--
Joe
|
|

March 7th, 2004, 09:17 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Quote:
quote:Originally posted by planoie
if(!text.value))
A) The syntax is wrong. It should be:
if(!text.value)
B) This test will only tell you if there is a valid "value" property. There will always be a valid value property, but it might be blank. So you must test accordingly:
if(text.value.length()==0)
You may also want to look into testing this field with a phone number regular expression.
Peter
------------------------------------------------------
Work smarter, not harder.
|
Surely length is a property not a method? :)
--
Joe
|
|

March 7th, 2004, 09:23 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
That is entirely likely, and most likely correct. 
(And heck, I just woke up. ;) Tnx.)
|
|

March 7th, 2004, 09:32 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
I have the advantage of having been up for five hours, must be time for my afternoon nap. Enjoy the rest of your weekend, you have more to go than me. :)
--
Joe
|
|

March 7th, 2004, 09:35 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Hah! Good point. I shouldn't even be here. It's beautiful outside. Alas, I am afflicted with a computer bug.
|
|

March 8th, 2004, 09:23 AM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
i still have one more problem. i change the button to the submit button. How am i going to stop the submit button to submit the form if the value is null.is it that i have to change it to
onclick="return check(document.myform.telephone.value)" or
onclick="return (check(document.myform.telephone.value))"
|
|

March 8th, 2004, 09:48 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Well no-one suggested you change it to a submit button:), when I said 'manually' I meant by using JavaScript rather than HTML.
If you do that then you'll need to return true if you want the default action to happen, in this case submit, or false to prevent it. Then move the validation code to the onsubmit event of the form:
Code:
<html>
<head>
<title>Testing</title>
<style>
#textbox {position:absolute; left:200px};
</style>
<script>
function check(textbox)
{
if(textbox.value == "")
{
alert("Please enter telephone number");
//textbox.select();
text.focus();
return false;
}
return true;
}
</script>
</head>
<body onload="document.myform.telephone.focus();">
<form name="myform" onsubmit="return check(document.myform.telephone)">
Telephone No. <input id="textbox" type="text" name="telephone" ><br><br>
<input type="submit" value="Submit"> <br><br>
</form>
</body>
</html>
--
Joe
|
|

March 8th, 2004, 08:58 PM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks joefawcett. But actually mine is a submit button but i ask the function empty i type it wrongly because my concern is the function. Sorry for that!
Now, since the form need to submit i have to change the form tag to
<form name="myform" onsubmit="return check(document.myform.telephone)" METHOD="POST" ACTION="try.html" >
however even when i put the
onsubmit="return check(document.myform.telephone)", or
onsubmit="return (check(document.myform.telephone))",
the form will be submitted no matter what if i type in the textbox or not typing anything or type wrongly. Meaning it will be submitted no matter what.
Why is it? And most importantly, how to solve it? HELP!
|
|
 |