Solved

Trouble with isAlphaNumeric() function

Posted on 2008-10-24
5
468 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

813 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

15 Experts available now in Live!

Get 1:1 Help Now