Javascript form validation pattern [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 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.

Related

For loop JS for a calculator [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
I'm trying to create a calculator which goes through 18 units. I wanted to make my code shorter by using a for loop. I thought something like this would work:
var i=0;
for (i=0;i<=18;i++)
{
if (Unit[i] = "P" or Unit[i] == "p")
{
UnitTotal[i] = 70;
SetCookie('UnitAns'[i],UnitAns[i]);
}
}
This doesn't work what am I doing wrong or what do I need to do differently?
Unit[i] = "P"
Unless it throws an exception because Unit isn't defined, this will always be true. = is an assignment, not a comparison.
or
or is not a keyword in JavaScript. The OR operator is ||.

JQuery Selector With Some Conditions [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
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.

how to deobfuscate javascript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
can any one tell me how to de-obfuscate this?
É=-~-~[],ó=-~É,Ë=É<<É,þ=Ë+~[];Ì=(ó-ó)[Û=(''+{})[É+ó]+(''+{})[ó-É]+([].ó+'')[ó-É]+(!!''+'')[ó]+({}+'')[ó+ó]+(!''+'')[ó-É]+(!''+'')[É]+(''+{})[É+ó]+({}+'')[ó+ó]+(''+{})[ó-É]+(!''+'')[ó-É]][Û];Ì(Ì((!''+'')[ó-É]+(!''+'')[ó]+(!''+'')[ó-ó]+(!''+'')[É]+((!''+''))[ó-É]+([].$+'')[ó-É]+'\''+''+'\\'+(... Masked for confidentiality reasons
Look for the "()" in the end. Those are for executing the obscured function code. If you remove the last one and use "toString()" instead in node you will get the following (After formatting a bit):
function anonymous() {
na = prompt('Entrez le mot de passe');
if(a == 'I changed this to not make it too easy for you' {
alert('bravo');
} else {
alert('fail...');
}
}
Try it yourself, but always be careful, since if you are not careful this kind of code can run harmful stuff on your computer.
PS: A few more words about how it actually works. Those weird french seeming letters everywhere are just variables, which are defined in the beginning. É for example has the value of 2, since using the bitwise not operator on an empty array results a -1, and -~-(-1) = 2. All those backslashes are then used in combination with this numeric variables to get characters which eventually form the code of the function.

Comparing string to a list in javascript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a list of car models that are in the list as Chevrolet,Ford,BMW, so I enacted this code on them.
cars.getMakes() returns a list with the names mentioned above and they are formatted as such.
cars.getAllModels() returns a similar list with just the car model names.
make is a single string that is either a model name or make name. Depends on users input.
I want to test to see if what the user put in actually exists in my predetermined lists.
if it does; true. If not; False.
function makeCheck(make) {
var models = cars.getAllModels();
var makes = cars.getMakes();
if (make == makes[make]) {
return true;
} else {
return false;
}
}
If you want to check that the variable make passed to the function is in the list of valid makes:
function makeCheck(make) {
var makes = cars.getMakes();
return (makes.indexOf(make) != -1);
}

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