p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Javascript How-To (http://p2p.wrox.com/forumdisplay.php?f=87)
-   -   check if empty (http://p2p.wrox.com/showthread.php?t=10192)

hosefo81 March 7th, 2004 07:38 AM

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>




planoie March 7th, 2004 09:04 AM

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.

joefawcett March 7th, 2004 09:07 AM

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

joefawcett March 7th, 2004 09:17 AM

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

planoie March 7th, 2004 09:23 AM

That is entirely likely, and most likely correct. [:o)]
(And heck, I just woke up. ;) Tnx.)

joefawcett March 7th, 2004 09:32 AM

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

planoie March 7th, 2004 09:35 AM

Hah! Good point. I shouldn't even be here. It's beautiful outside. Alas, I am afflicted with a computer bug.

hosefo81 March 8th, 2004 09:23 AM

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


joefawcett March 8th, 2004 09:48 AM

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

hosefo81 March 8th, 2004 08:58 PM

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!




All times are GMT -4. The time now is 09:08 AM.

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