• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 487
  • Last Modified:

Trouble with isAlphaNumeric() function

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
newbieweb
Asked:
newbieweb
  • 2
  • 2
3 Solutions
 
chaitu chaituCommented:
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
 
sijishJohnCommented:
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
 
chaitu chaituCommented:
this also works;

function isAlphaNumeric(val)
{
if (val.match(/^[a-zA-Z0-9]+$/))
{
return true;
}
else
{
return false;
}
}
0
 
sijishJohnCommented:
change 'isAlphaNumeric(elem.Value)' to isAlphaNumeric(elem.value)
0
 
David S.Commented:
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now