Nevin
Nevin

Reputation: 3298

Uncaught TypeError: Object [object RegExp] has no method 'match'

Basically am ending up with the error on the console:

Uncaught TypeError: Object [object RegExp] has no method 'match'

This is the code that triggers the error:

addMember = function addMember(e) {
    console.log(e.target);
    var s1 = new SchoolAdmission();
    s1.name = document.getElementById('name').value;
    s1.age = document.getElementById('age').value;
    s1.department = document.getElementById('department').value;
    s1.display(e.target.id);
};

validate = function validate(e) {

    if (nameInput.value == "") {
        (errorInput).innerHTML = "Please fill all columns";
        validation = false;
    }
    else if (!nameInput.match(charFilter)) {
        (errorInput).innerHTML = "Please enter only alphabets";
        validation = false;
    }

    if (ageInput.value == "") {
        (errorInput).innerHTML = "Please fill all columns";
        validation = false;
    }
    else if (!ageInput.match(numberFilter)) {
        (errorInput).innerHTML = "Please enter only numbers";
        validation = false;
    }

    if (validation) {
        addMember(e);
    }
};

I have made a validation for my app that am building. I have just applied it on the "Add Student" button for now and move it on to the "Add Teacher" later on.

<button id="addStudent" class="btn btn-success" onclick="validate(event)">Add Student</button>

Here's my fiddle : http://jsfiddle.net/LPu9x/6/

My issues are :

1) Resolving this error.

2) Is my validation method right?

Upvotes: 1

Views: 1111

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

nameInput is dom object so it does not have the match method, you need to get the value of the input then use .match

nameInput.value.match(charFilter)

Upvotes: 1

Related Questions