Checking if a string contains a specific substring:
Function: $.inArray()
Example:
var fruits = ['apple', 'banana', 'orange'];
var searchFruit = 'banana';
if ($.inArray(searchFruit, fruits) !== -1) {
console.log('Fruit found!');
} else {
console.log('Fruit not found!');
}
Expected Output: "Fruit found!"
Checking if a string starts with a specific substring:
Function: String.startsWith()
Example:
var sentence = 'Hello, world!';
if (sentence.startsWith('Hello')) {
console.log('Sentence starts with Hello');
} else {
console.log('Sentence does not start with Hello');
}
Expected Output: "Sentence starts with Hello"
Checking if a string ends with a specific substring:
Function: String.endsWith()
Example:
var sentence = 'Hello, world!';
if (sentence.endsWith('world!')) {
console.log('Sentence ends with world!');
} else {
console.log('Sentence does not end with world!');
}
Expected Output: "Sentence ends with world!"
Checking if a string is empty or contains only whitespace:
Function: $.trim()
Example:
var userInput = ' ';
if ($.trim(userInput) === '') {
console.log('Input is empty or contains only whitespace');
} else {
console.log('Input is not empty');
}
Expected Output: "Input is empty or contains only whitespace"
Checking if a string matches a regular expression pattern:
Function: RegExp.test()
Example:
var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
var userEmail = 'user@example.com';
if (emailPattern.test(userEmail)) {
console.log('Email is valid');
} else {
console.log('Email is not valid');
}
Expected Output: "Email is valid"
Checking if a value is numeric:
Function: $.isNumeric()
Example:
var inputValue = '123';
if ($.isNumeric(inputValue)) {
console.log('Input is numeric');
} else {
console.log('Input is not numeric');
}
Expected Output: "Input is numeric"
These are just a few examples, and there are many more functions and methods in jQuery that you can use for validation depending on your specific requirements.
Checking if a checkbox or radio button is checked:
Function: :checked selector
Example:
<input type="checkbox" id="agreeCheckbox">
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script>
$(document).ready(function() {
if ($('#agreeCheckbox').is(':checked')) {
console.log('Checkbox is checked');
} else {
console.log('Checkbox is not checked');
}
});
</script>
Expected Output: "Checkbox is checked" or "Checkbox is not checked"
Checking the length of a string or array:
Function: length property
Example:
var inputText = 'Hello, world!';
if (inputText.length > 10) {
console.log('Input has more than 10 characters');
} else {
console.log('Input has 10 or fewer characters');
}
Expected Output: "Input has more than 10 characters" or "Input has 10 or fewer characters"
Checking if an input field is not empty:
Function: $.trim()
Example:
<input type="text" id="usernameInput">
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script>
$(document).ready(function() {
var username = $.trim($('#usernameInput').val());
if (username !== '') {
console.log('Username is not empty');
} else {
console.log('Username is empty');
}
});
</script>
Expected Output: "Username is not empty" or "Username is empty"
Checking if a value is a valid date using a library (e.g., Moment.js):
Function: moment() function from Moment.js
Example:
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script>
$(document).ready(function() {
var dateValue = '2022-01-15';
if (moment(dateValue, 'YYYY-MM-DD', true).isValid()) {
console.log('Date is valid');
} else {
console.log('Date is not valid');
}
});
</script>
Expected Output: "Date is valid" or "Date is not valid"
These examples showcase different validation scenarios using jQuery. Depending on your specific needs, you can combine these techniques to create comprehensive validation for your web application.
Checking if a string contains a specific substring (case insensitive):
Function: String.toLowerCase()
Example:
var text = 'This is a Sample Text';
var substring = 'sample';
if (text.toLowerCase().includes(substring.toLowerCase())) {
console.log('Substring found in the text');
} else {
console.log('Substring not found in the text');
}
Expected Output: "Substring found in the text" (case insensitive)
Checking if an input value contains only letters:
Function: Regular Expression (/^[a-zA-Z]+$/)
Example:
<input type="text" id="nameInput">
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script>
$(document).ready(function() {
var name = $.trim($('#nameInput').val());
if (/^[a-zA-Z]+$/.test(name)) {
console.log('Input contains only letters');
} else {
console.log('Input contains non-letter characters');
}
});
</script>
Expected Output: "Input contains only letters" or "Input contains non-letter characters"
Checking if an input value is a valid email address:
Function: Regular Expression (/^[^\s@]+@[^\s@]+\.[^\s@]+$/)
Example:
<input type="text" id="emailInput">
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script>
$(document).ready(function() {
var email = $.trim($('#emailInput').val());
if (/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
console.log('Email is valid');
} else {
console.log('Email is not valid');
}
});
</script>
Expected Output: "Email is valid" or "Email is not valid"
Checking if an input value is a valid URL:
Function: Regular Expression (/^(ftp|http|https):\/\/[^ "]+$/)
Example:
<input type="text" id="urlInput">
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script>
$(document).ready(function() {
var url = $.trim($('#urlInput').val());
if (/^(ftp|http|https):\/\/[^ "]+$/.test(url)) {
console.log('URL is valid');
} else {
console.log('URL is not valid');
}
});
</script>
Expected Output: "URL is valid" or "URL is not valid"
These examples demonstrate additional validation scenarios using jQuery and cover checking substrings, input content, and common patterns like letters-only, email addresses, and URLs. Adjust the regular expressions and conditions based on your specific requirements.
Top comments (0)