Pattern validation with JavaScript

Viewed 25517

I want a pattern with letters and numbers only.

This is how I do it...

JavaScript file:

var pattern_checked = checkPattern(); 

function checkPattern(){
        var elem = document.getElementById("name");

        var pattern = elem.getAttribute("[a-zA-Z0-9_]");
        var re = new RegExp(pattern);
        if (re.test(elem.value)) {
            return true;
        } else {
            return false;
        }
    }

But in both the cases, I'm getting false.

What is wrong in this code?

2 Answers
Related