(index):128 Uncaught TypeError: MobileNumberVal.test is not a function
I am getting an error (index):128 Uncaught TypeError: MobileNumberVal.test is not a function while using !MobileNumberVal.test(regexMobile) in my function while validating the mobile number on form submit.
Here is my code. Give me a solution for the error.
HTML Input element
<input type="text" class="form-control" name="mobile" id="mobileNumber">
Javascript code
var mobileNumberElem = $('#mobileNumber');
var MobileNumberVal = mobileNumberElem.val();
var regexMobile = /^\d{10}$/;
if(!MobileNumberVal.test(regexMobile)){
showErrorMsg(mobileNumberElem, 'Please provide accurate mobile number !');
isValid = false;
}
-
Replace !MobileNumberVal.test(regexMobile) with !regexMobile.test(MobileNumberVal)
You have to just replace the code !MobileNumberVal.test(regexMobile) with !regexMobile.test(MobileNumberVal) in your script. Your code will look like below.
var mobileNumberElem = $('#mobileNumber'); var MobileNumberVal = mobileNumberElem.val(); var regexMobile = /^\d{10}$/; if(!MobileNumberVal.test(regexMobile)){ showErrorMsg(mobileNumberElem, 'Please provide accurate mobile number !'); isValid = false; }
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Jquery
- How to create read more in jquery
- How to pass javascript variable to laravel route on ajax call
- How to preview image before uploading in jQuery
- Uncaught TypeError: $.ajax is not a function
- Uncaught TypeError: $.ajaxSetup is not a function
- How to find closest upper element and append html to below it in jquery
- How to use and purpose of localstorage in javascript
- How to remove Choose File button in jquery
- Show loading icon on click submit button in jQuery
- Execute function after Ajax call is complete
- How to reset form input element on click
- JQuery find text and show as bold in html page
- Mixed Content: The page at 'https://w3codegenerator.com/' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint
- Jquery-3.6.0.min.js CDN link
- Preview image in base64 format using jQuery on change input file
- Add and remove input fields on click button using jQuery
- Get the href link on click anchor in javascript
- Validate form using jQuery
- Ajax for multiple form
- JQuery append html to below the target element
- How to check object is empty or not in jQuery
- Enable click event on appended element using jQuery
- How to check ajax request is completed in jQuery
- How to get closest form input value in jQuery
- Preview image in BLOB URL format on choose file in jQuery