?
Solved

Trouble with isAlphaNumeric() function

Posted on 2008-10-24
5
Medium Priority
?
483 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 20

Accepted Solution

by:
chaitu chaitu earned 800 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 800 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 400 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…
Suggested Courses

777 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