Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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>



Reply With Quote
  #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.
Reply With Quote
  #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
Reply With Quote
  #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
Reply With Quote
  #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.)
Reply With Quote
  #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
Reply With Quote
  #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.
Reply With Quote
  #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))"

Reply With Quote
  #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
Reply With Quote
  #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!


Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 08:44 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.