Solved

Trouble with isAlphaNumeric() function

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to make google charts responsive 4 54
Bootstrap question 4 33
Accordion won't close correctly 5 14
Jquery driving me nuts... 14 29
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

730 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