JQuery Selector With Some Conditions [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
JQuery selector with multiple conditions Email Validation
hi i have to validate Email address.
my following code works fine:
contact_email = $('#contact_email').val();
var reg = /^([A-Za-z0-9_\-\.])+\#([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
//if Email address is empty it allowed but if it have any value then check whether proper email address or not
if(contact_email && !reg.test(contact_email)) {
err += 1;
$('#contact_email').addClass('boxerror');
}
it works but following code not work
contact_email = $('#contact_email').val().is(reg.test(this.val())).addClass('boxerror');
1.what mistake i had done?
2.JQuery with selector with multiple condition is possible

You are misunderstanding how .is(...) works. You have to pass a jquery selector in is() function, or you can also pass a function which will be called by jquery for all the function. And it will return a boolean value.
To accomplish your task you will be needed to use filter(...).
Here is the code to help you :
$("#contact_email").filter(function(index, element) {
return !reg.test(element.value);
}).addClass("boxerror");
Full documentation for filter() is here
Hence :
Mistake: you were using is() in wrong situation.
selector with multiple condition is possible if implemented jquery nicely.

Related

I cant manage to put my javascript function to work [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
Improve this question
Im creating accounts on a api and the password needs those restrictions.The handleRegist() is connecting to the api but the passwords i use always goes through that if cycle.I am using a function to check the password strength for a login the password must contain at least 1 special character 1 uppercase letter and it must be longer than 8 characters.
I have this code so far:
function passwordChecker(password){
var strongPassword = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,15}$/;
if (password.value.match(strongPassword)) {
console.log("Good Password");
handleRegist();
}else
console.log("Try Again!\nnot strong enough");
}
NOTE: the strongPassword variable i found it on the internet and i am not sure if the restriction works like that.
To be able to fire the function you need to call it from somewhere, naturally, you would like to call it inside the onchange event inside the input component.
<input
type="text"
onChange={(e) => passwordChecker({ value: e.target.value })}
/>
Here is a working codesandbox example:
Working example
(open the console from the bottom of the page to see the result while typing inside the input)
Notice: im passing the value inside an object {value: e.target.value}, im doing it because inside your function you are checking password.value.match(strongPassword), it expect to get password.value, therefore i needed to add the value inside an object and pass it to the function.
i recommend to handle the value and store it in a state with useState and pass the state as value to the input.

how to pass a variable to queryselectorAll? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Hi I want to pass a variable to queryselectorAll. I tried a couple of things but non have worked for me. I'm doing that because in my code I change the variable to a new one every time a click a button.
var container = document.getElementById(containerBox.id)
// I want containerBox.id to be called in querySelectorAll
templateDiv = document.querySelectorAll('#' + 'containerBox.id' + 'template')[0].content.firstElementChild
Thanks in advance
This question can be simplified to: How do I construct a string from fixed parts and a variable part?
The answer to that is:
'#' + containerBox.id + 'template'
(i.e. just don't put quotes around your variable name).
But why bother using .querySelectorAll() just to grab the first index? You could simply call .querySelector() instead.
And if all you need to get is an element with a specified id, you can just do this:
document.getElementById(containerBox.id + 'template')
... which is the method you're already using in your first line.

Javascript form validation pattern [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to validate that two characters and a number were correctly input.
var studentValid = /^[MTWTF][AL][1-9]$/i;
if (studentValid.test(studentTemp.value))
{
alert("true");
}
else
{
alert("false");
}
Yet everything I enter turns out false?
The problem is with your regexp (/^[MTWTF][AL][1-9]$/i). What this tells you is that you first want one of the characters M,T,W,T or F, and after that either A or L and finaly a number (and nothing before or after this).
So for example
ML4, WA5, FL9
will give you true
while
AM9, ML0, MMA5, MA99
will give you false.
Is this the pattern you are trying to match? There is nothing else wrong with your code and a valid value will give you true, for example:
var studentValid = /^[MTWTF][AL][1-9]$/i;
var value = 'MA9';
if (studentValid.test(value))
{
alert("true");
}
else
{
alert("false");
}
When working with regexp, it can be very usefull to use a tool to help you build it, check out https://regex101.com/r/A5FOIh/3 where you can try your different studentTemp.value to see if they match.

jQuery filter options with value greater (less) than variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need to disable values of options in select that are less than some number, which I do not know beforehand (it is set in another form element). I need something like code below, but with variable value of "variableInteger":
select.children().filter(function() {
return $(this).attr("value") > variableInteger;
}).each(function () {$(this).attr('disabled', 'disabled')});
Is there some clever way to do it?
PS. variableInteger value is from another form element, which name is also known only at runtime.
No need for the .each, also make use of prop and this.value (no need for $(this).attr("value");)
select.children("option").filter(function() {
return this.value > variableInteger;
}).prop("disabled", true);

How to find a certain class using .find() or some jQuery Syntax? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
if( hasWhiteSpace($(this).find(".nospace:input").val()) ){
$(this).find(".nospace:input").filter(function() {
return hasWhiteSpace(this.value) == true;
}).addClass("blank");
setError(".motd_register_company","There must be no space in between.");
return false;
}
There is something wrong about my code above, and the code only validate the first input, while the others not, how could I validate all the "nospace" class that I have in my form?
can you guys trim my code if there is something wrong.
Are you trying to recursively search elements for a particular classname? If so:
$('input.class_name').each(function() {
// Do you stuff to this input element
});
jQuery.each("input.nospace", function(i, el){
var self = $(el);
if(hasWhiteSpace(self.val()) self.addClass("blank");
});

Categories

Resources