Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
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
  #1 (permalink)  
Old March 7th, 2004, 07:38 AM
Authorized User
 
Join Date: Oct 2003
Location: KL, wilayah persekutuan, Malaysia.
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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>



  #2 (permalink)  
Old March 7th, 2004, 09:04 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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.
  #3 (permalink)  
Old March 7th, 2004, 09:07 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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
  #4 (permalink)  
Old March 7th, 2004, 09:17 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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
  #5 (permalink)  
Old March 7th, 2004, 09:23 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

That is entirely likely, and most likely correct.
(And heck, I just woke up. ;) Tnx.)
  #6 (permalink)  
Old March 7th, 2004, 09:32 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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
  #7 (permalink)  
Old March 7th, 2004, 09:35 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Hah! Good point. I shouldn't even be here. It's beautiful outside. Alas, I am afflicted with a computer bug.
  #8 (permalink)  
Old March 8th, 2004, 09:23 AM
Authorized User
 
Join Date: Oct 2003
Location: KL, wilayah persekutuan, Malaysia.
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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))"

  #9 (permalink)  
Old March 8th, 2004, 09:48 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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
  #10 (permalink)  
Old March 8th, 2004, 08:58 PM
Authorized User
 
Join Date: Oct 2003
Location: KL, wilayah persekutuan, Malaysia.
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Recordset Empty Check Coby Access VBA 2 April 27th, 2007 04:09 PM
best way to check for variable being empty crmpicco Classic ASP Basics 3 March 28th, 2006 12:11 PM
How to check for empty fields in a form. crmpicco Javascript How-To 7 February 15th, 2005 10:38 AM
How to check if you have an empty array Ciarano VB How-To 9 March 31st, 2004 09:17 AM
How to check if collection is empty Ciarano Beginning VB 6 2 March 5th, 2004 08:11 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.