Solved

Trouble with isAlphaNumeric() function

Posted on 2008-10-24
5
466 Views
Last Modified: 2012-06-21
I have a problem with this function but can not yet debug into JavaScript.  Can anyone find the problem?

It does not seem to matter what characters I enter to the text field, the isAlphaNumeric() function seems to always return true.

newbieweb

    function isAlphaNumeric(str){
        return /^[a-zA-Z0-9]+$/.test(str);
    }

    function CheckSize()
    {
        var elem = document.getElementById("recordLocatorTextBox");
        if (elem != null)
        {
            if (!isAlphaNumeric(elem.Value))
            {
                  // it never gets here...
            }
        }
    }
0
Comment
Question by:newbieweb
  • 2
  • 2
5 Comments
 
LVL 20

Accepted Solution

by:
chaitu chaitu earned 200 total points
ID: 22801933
function isAlphanumeric(elem, helperMsg){
      var alphaExp = /^[0-9a-zA-Z]+$/;
      if(elem.value.match(alphaExp)){
            return true;
      }else{
            alert(helperMsg);
            elem.focus();
            return false;
      }
}
0
 
LVL 4

Assisted Solution

by:sijishJohn
sijishJohn earned 200 total points
ID: 22801935
try this...
function CheckSize()

    {

        var elem = document.getElementById("recordLocatorTextBox");

        if (elem != null)

        {

            if (!isAlphaNumeric(elem.value))

            {

                  // it never gets here...

            }

        }

    }

Open in new window

0
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 22801937
this also works;

function isAlphaNumeric(val)
{
if (val.match(/^[a-zA-Z0-9]+$/))
{
return true;
}
else
{
return false;
}
}
0
 
LVL 4

Expert Comment

by:sijishJohn
ID: 22801940
change 'isAlphaNumeric(elem.Value)' to isAlphaNumeric(elem.value)
0
 
LVL 42

Assisted Solution

by:David S.
David S. earned 100 total points
ID: 22801984
As sijishJohn said, the isAlphaNumeric function is fine. The problem is the argument passed to it. "elem" does not have a "Value" property, but it doesn't have a "value" property.

The RegExp.test() method is more efficient than the String.match() method.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now