Vuiee
Vuiee

Reputation: 39

jquery phone number validation with "match" error

I am working on validating the phone number using jquery in the format of:

+XX-XXXX-XXXX
+XX.XXXX.XXXX
+XX XXXX XXXX 

However,it is giving me an error in the console:

Uncaught TypeError: Cannot read property 'match' of undefined

function phonenumber(inputtxt) {
	var phoneno = /^\+?([0-9]{2})\)?[-. ]?([0-9]{4})[-. ]?([0-9]{4})$/;
	if(inputtxt.value.match(phoneno)) {
		return true;
		  }  
	else {  
		 alert("message");
		 return false;
		  }
		}
	jQuery(".btnchec").click(function(){
		var inputtxt = jQuery("#inputcpf").val();
		console.log(phonenumber(inputtxt));
		});
<fieldset>
	 <legend>Validation</legend>
	 <div style= "padding-top:15px;">
		 <label style= "padding: 20px; font-size: 16px; font-weight:600; color: #AA6903;">ID: </label>
		 <input type="text" id="inputcpf" name="cpf" size="18" maxlength="18" autofocus>
		  <input type="button" style= "margin-top:10px;" class="btnchec" name="Submit" value="Check">
		  </div>
</fieldset>

Upvotes: 0

Views: 187

Answers (1)

Sean Wessell
Sean Wessell

Reputation: 3510

inputtxt is a string and therefor does not have a .value property.

Change:

if(inputtxt.value.match(phoneno))

TO:

if(inputtxt.match(phoneno))

Upvotes: 2

Related Questions