(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
- Mixed Content: The page at 'https://w3codegenerator.com/' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint
- Show and hide target element on click button using javascript/jquery
- Jquery-3.6.0.min.js CDN link
- Ajax for multiple form
- Execute function after Ajax call is complete
- Get the href link on click anchor in javascript
- Show loading icon on click submit button in jQuery
- How to remove Choose File button in jquery
- How to use and purpose of localstorage in javascript
- How to create read more in jquery
- How to validate input type file size in jQuery for multiple records
- Append method to add text to body not working
- Ajax done fail ajaxcomplete in not a function
- How to find closest upper element and append html to below it in jquery
- Preview image in base64 format using jQuery on change input file
- Uncaught TypeError: $.ajax is not a function
- How to reset form input element on click
- How to pass javascript variable to laravel route on ajax call
- JQuery append html to below the target element
- Preview image in BLOB URL format on choose file in jQuery
- How to check object is empty or not in jQuery
- How to preview image before uploading in jQuery
- Jquery.validate.min.js:4 Uncaught TypeError: Cannot read properties of undefined (reading 'mode')
- Validate form using jQuery
- Enable click event on appended element using jQuery