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 1st, 2004, 09:54 AM
Authorized User
 
Join Date: Oct 2003
Location: KL, wilayah persekutuan, Malaysia.
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
Default validating user input

i want to check if user enter a string in the textbox but the code below has error.
Please help me correct it.

<html>
<head>
    <title>Testing</title>
    <style>
        #textbox {position:absolute; left:200px};
    </style>
    <script>
        function validate(textbox)
        {
            if(isNumber(textbox.value))
            {
                alert("Please enter string");
                textbox.select();
                textbox.focus();
            }
        }
    </script>
</head>
<body onload="document.myform.custfname.select(); document.myform.custfname.focus();">
<form name="myform" >

Customer First Name <input id="textbox" type="text" name="custfname"><br><br>

<input type="button" value="Submit" onclick="validate(myform.custfname)">

</form>
</body>
</html>



  #2 (permalink)  
Old March 1st, 2004, 10:14 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

There is no javascript function called <s>isNumeric</s>isNumber. I would use a regular expression for this:
Code:
function validate(textbox)
{
    if ( /[^a-zA-Z]/.test(textbox.value) )
    {
         alert("Please enter string");
         textbox.select();
         textbox.focus();
    }
}
hth
Phil
  #3 (permalink)  
Old March 1st, 2004, 10:15 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

What is the error?
What does your isNumber function look like and what do you want it to do?
Do you want to test to see if it is a valid number or just contains digits?

Also there is no point selecting and then focusing. Select highlights all the text, focus just puts the caret in the text box at position one.

--

Joe
  #4 (permalink)  
Old March 1st, 2004, 11:34 AM
Authorized User
 
Join Date: Oct 2003
Location: KL, wilayah persekutuan, Malaysia.
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The status bar display error and nothing happens when i clicked the button.Well, i learn on a book of javascript that i can check whether the user enter digit in the textbox.The select()and focus() function is also from the book that say it use together to highlight a desired textbox.

Actually what i want to do is to test if the value entered is digit. Because i have a few textbox and i want to test if the user entered valid value.For example, if a textbox requires digit and the user entered string, then i would like to select the textbox to let the user enter it.
And how can i check if it is a string? what's the function?
Could you please show me by adding how to test is a digit and test is a string in the textbox?

  #5 (permalink)  
Old March 1st, 2004, 02:01 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Use typeof():

alert(typeof variable);

HTH,

----------
---Snib---
----------
  #6 (permalink)  
Old March 2nd, 2004, 05:35 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

This should test for a numeric value:
Code:
function isNumeric(Value)
{
  var oRE = /^[-+]?(\d*\.)?\d+([eEdD][-+]?\d+)?$/;
  return oRE.test(Value);
}
You may just want integer values though.

--

Joe
  #7 (permalink)  
Old March 3rd, 2004, 07:53 AM
Authorized User
 
Join Date: Oct 2003
Location: KL, wilayah persekutuan, Malaysia.
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks guys. i finally make it. joefawcett's function does'nt work for me. but thanks for helping anywhere.Here's the code that work for me.
<html>
<head>
    <title>Testing</title>
    <style>
        #textbox {position:absolute; left:200px};
    </style>
    <script>
    function isDigit (c)
    {
            return ((c >= "0") && (c <= "9"))
    }

    function validate(obj)
    {
           value = obj.value;
            for (n = 0; n < value.length; n++)
               if ( !isDigit(value.charAt(n)))
         {
            obj.select();
                 obj.focus();
                 alert("Field must be numeric");
                   return;

               }
    }
    function valid(textbox)
    {
          if ( /[^a-zA-Z]/.test(textbox.value) )
            {
                 alert("Please enter string");
                 textbox.select();
                 textbox.focus();
            }
    }

</script>
</head>
<body onload="document.myform.custfname.select(); document.myform.custfname.focus();">
<form name="myform" >
Telephone No. <input id="textbox" type="text" name="custfname" onBlur="validate(this);"><br><br>

Customer Last Name <input id="textbox" type="text" name="custlname" onBlur="valid(this);"><br><br>

<input type="button" value="Submit" > <br><br>
</form>
</body>
</html>



  #8 (permalink)  
Old March 3rd, 2004, 08:09 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

If you just wanted to allow integers only in your telephone number field you could use a simple regexp in your validate() function, like this:
Code:
    function validate(obj) 
    {
          if ( /[^0-9]/.test(obj.value) )
            {
                 alert("Field must be numeric.");
                 obj.select();
                 obj.focus();
            }
    }
  #9 (permalink)  
Old March 3rd, 2004, 08:19 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 hosefo81
 Thanks guys. i finally make it. joefawcett's function does'nt work for me.

Tried this code and it works fine, if you'd said you wanted to check for a phone number I'd have made it simpler, originally you said you were checking the customer's forename...
[code]
function isNumeric(Value)
{
var oRE = /^[-+]?(\d*\.)?\d+([eEdD][-+]?\d+)?$/;
return oRE.test(Value);
}

function testValidator(arrTest)
{
  var sResult = "";
  for (var i = 0; i < arrTest.length; i++)
  {
    var sTest = arrTest[i];
    sResult += sTest + " => " + isNumeric(sTest) + "\n";
  }
  alert(sResult);
}


testValidator(["0123456", "123a456", "0.123", "-1.1234", "-123456", "-.12345", "123.45E2"]);

[code]



--

Joe
  #10 (permalink)  
Old March 3rd, 2004, 08:21 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 pgtips
 There is no javascript function called <s>isNumeric</s>isNumber. I would use a regular expression for this:
Code:
function validate(textbox)
{
    if ( /[^a-zA-Z]/.test(textbox.value) )
    {
         alert("Please enter string");
         textbox.select();
         textbox.focus();
    }
}
hth
Phil
As a side issue this sort of regular expression doesn't cope well with characters that have diacritical marls etc.

--

Joe




Similar Threads
Thread Thread Starter Forum Replies Last Post
Validating Input from a Datasheet using VBA digby_dog Access VBA 1 October 6th, 2008 10:35 AM
Displaying user input macrocosm Beginning PHP 3 June 15th, 2006 09:30 PM
user password validating cooky4 VB How-To 15 May 4th, 2005 08:45 AM
validating input bukky VBScript 1 April 1st, 2004 04:30 AM
Validating user input stu9820 VB.NET 2002/2003 Basics 2 January 15th, 2004 12:51 PM





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