Use Logical Expressions In A Switch / Case?
Aug 31, 2009
I'm doing a bit of experimentation and would like a bit of advice if possible please.
In my switch statement I would like to use the || or operator, it works correctly it I set the case to 31, but if I try 1||31 it doesn't.code...
View 4 Replies
ADVERTISEMENT
Jul 20, 2005
Is this sort of thing possible:
var X = 'Moe'
switch (X) {
case 'Curly'||'Moe'||'Larry':
alert('Found one of the Three Stooges');
case 'Chico'||'Harpo'||'Zeppo'||'Grouco'||'Gummo':
alert('Found one of the Marx Brothers');
default:
alert('No matches');
This gives 'No matches' unless I only put a single string in the 'case'
lines. I've just been using VB's Select Case which is a similar flow
control but which allows conditional arguments in the 'cases'. I just
wondered...
I realise you could put each set of names in an array and iterate
through each array, but that's a different issue.
View 4 Replies
View Related
Nov 19, 2010
So, I have a list of items that need to have a new preset list item appear based on what day it is. I have the date script working (to test, change the first case to some random date and change the third case to todays date - 10192010 - It will fire that document.write). What I need the cases to do though, is set the visibility of certain list items.
This is just an example, there will be around 20 list items in the final project. As you can see in the first two cases, I've tried a couple different routes to no avail.
The Code (class references are irrelevant to this example, they belong to the final project):
<html>
<head>
<script type="text/javascript" language="JavaScript">
/*
[Code]....
View 2 Replies
View Related
Oct 18, 2009
I am returning the present time - then using the .substr to remove all digits except the last two on the right (i.e effectively returning the value 32 from 65344532) I am then looping, subtracting 11 from that number until the value is less than 11. The intent being to then return the value from the appropriate matching array ID code...
The problem arises with evaluating two digit numbers beginning with zero - In cases where the last two numbers are greater than 09, the looping returns a 1 digit number for valuse less than 10, in cases where the last two digits begin with zero the loop will not begin. I have attempted to use the switch(n) to determine if any 01, 02, 03 ... etc exists but this is not evaluating correctly - is this due to using the date/time object and if so is there a good way to convert this to either a numeric or string datatype where the case can be evaluated correctly?
View 11 Replies
View Related
Sep 23, 2008
I would like to do a switch in javascript, with multiple values in the case. code...
View 14 Replies
View Related
Sep 14, 2010
how to get this functionality going. I have a div name "footer". Within "footer" I have 4 links:
link1, link2, link3, link4
Is there anyway to write a function which will sense which link was clicked and then alert the id of that selected link. I have written the code but am not certain why isn't it working. here is my code.
$('#footer a').bind('click', function(){
//alert($(this).attr('id'));
var mId = $(this).attr('id');
switch(mId)
[Code]....
View 4 Replies
View Related
Sep 7, 2010
I know this does not work, but hopefully you can see what I am trying to do. Only the first value is tested, but I want all of them to be.
[Code]...
Pretend there are like 10 more cases with more numbers with them. Currently this will only text to see if x is 0 or 42. How do I get it to test for the other numbers?
View 7 Replies
View Related
Jul 1, 2010
Ok, this seems easy, but I am struggling. I have this...
$("a[href$='mp4']").click(function () {
But I want it to work for mp4 or mov or more. Something like...
$("a[href$='mp4'||href$='mov']").click(function () { <-- which doesn't work apparently
View 2 Replies
View Related
Jul 14, 2010
What kind of logical error am I making? I want the alert(); to execute if both of the variables (cjob and czip) are blank, but the only way I can get it to work is if I replace && with ||.
if(trim(cjob.value) && trim(czip.value) == '')
{
alert('Hello');
}
View 2 Replies
View Related
Mar 4, 2009
is there anyway of distinguishing between whether the button is clicked directly by a mouse or its click event has been triggered logically?
<input type="button" id="someID" value="dene" onclick="alert('something');" />
<input type="button" value="dene2" id ="someID2" onclick="document.getElementById('someID').click()" />
i want some kind of thing : when you directly click on someID button, it wont do anything, but when you click on someID2 button, it will alert.
View 2 Replies
View Related
May 1, 2011
How can we identify logical errors from other types of errors?
View 4 Replies
View Related
Mar 4, 2009
Is there anyway of distinguishing between whether the button is clicked directly by a mouse or its click event has been triggered logically?
I want some kind of thing : when you directly click on someID button, it wont do anything, but when you click on someID2 button, it will alert.
View 2 Replies
View Related
May 10, 2005
I am using a JS form validator that uses the following regex to test whether a string contains at least one unsigned int and one letter.
var reAlphanumeric = /^(?=.*[0-9]+.*)(?=.*[a-zA-Z]+.*)([0-9a-zA-Z])+$/;
function checkAlphaNum( theField ) {
if ( isWhitespace( theField.value ) || !reAlphanumeric.test( theField.value ) ) {
return false;
} else {
return true;
}}
Since IE5 doesn't support look ahead expressions, I am using the following function to get the same result in IE5.
var reUnsignedInteger = /^[+]?[0-9]+$/;
var reAlphabetic = /^[a-zA-Z]+$/;
function IE_AlphaNum( s ) {
var isInt = false;
var isLet = false;
var isOp = false;
var len;
var setStr = strArray( s );
for ( var i = 0; i < setStr.length; i++ ) {
if ( reAlphabetic.test( setStr[i] ) ) {
isLet = true;
break;
}
}
for ( var i = 0; i < setStr.length; i++ ) {
if ( reUnsignedInteger.test( setStr[i] ) ) {
isInt = true;
break;
}
}
for ( var i = 0; i < setStr.length; i++ ) {
if ( !reAlphabetic.test( setStr[i] ) && !reUnsignedInteger.test( setStr[i] ) ) {
isOp = true;
break;
}
}
if ( isInt && isLet && !isOp )
return true;
else
return false;
}
Can a regex be used that will satisify the one int/one letter rule that will work in IE5 or if not, can the above function be refactored w/o having to interate through the string array each time? Neither method works in Opera 6. Also, is there any condition except for validating perhaps a password where such a rule would be needed?
View 1 Replies
View Related
Jul 23, 2005
Coding patterns for regular expressions is completely unintuitive, as far
as I can see. I have been trying to write script that produces an array
of attribute components within an HTML element.
Consider the example of the HTML element TABLE with the following
attributes producing sufficient complexity within the element:
<table id="machines" class="noborders inred"
style="margin:2em 4em;background-color:#ddd;">
Note that the HTML was created as a string in code, and thus there are NO
newlines ('
') in the string, as if a file was parsed...so newlines are
not an issue. The only whitespace is the space character ' ' itself,
required to delimit the element components.
I want to write an RE containing paranthesized substring matching that
neatly orders attribute components. The resulting array, after the
execution of the string .match() method upon the example, should look as
follows:
attrs = [ "id", "machines", "class", "noborders inred", "style",
"margin:2em 4em;background-color:#ddd;" ]
I can then march down the array (in steps of 2) setting attributes
(name=value) to the element using standard DOM interface methods, right?
In approaching the writing of the RE, I have to take into account the
characters permitted to form the attribute name and the attribute value.
I assume a start to the RE pattern as:
<attribute name>=<attribute value>
I then try to find the right RE pattern for <attribute name>, keeping in
mind what the legal characters are for attribute names according to the
HTML standard ("recommendation"):
[A-Za-z0-9-]+
I believe this patterns conforms to the standard for attribute values:
[,;'":!%A-Za-z0-9s.-]+
That pattern tries to be more exclusive than inclusive, although I think
just about every character on the planet, including a newline, is
acceptable in an attribute value, at least the kind one might see in an
HTML document. Code:
View 7 Replies
View Related
May 24, 2007
I am trying to construct a reg exp for a field which can accept username as
(username) or (username@domain.com/net/org etc).
username should allowed alphanumeric values also it should accept -,_,.
it should not allowed @ twice
View 2 Replies
View Related
Jul 5, 2004
I'm having a problem making the regular expression for U.S. zip code verification work.
the regular expression: /(^d{5}$)|(^d{5}-d{4}$)/
My code is below. No matter what I type in, it asks me to correct my zip code. Any suggestions? Code:
View 2 Replies
View Related
Jun 3, 2001
I am trying to use regular expressions within JavaScript for the picture upload component of a shopping cart. I still can't seem to get my mind around them.
I have a page with a working file upload. When you click on browse and then select your file, the file name is also returned to the second field, the picture name field. The problem is that the entire string is returned - 'G:CatalogMyPicturessomepic.jpg' instead of 'somepic.jpg'.
I know this can be pulled out with regular expressions, everything from the right until it hits a / or I included the script and a couple of links. Code:
View 3 Replies
View Related
May 6, 2010
know there is a similar post on here but it didn't really give me any actual help on this topic:It seems that I have the correct regular expressions in this however it doesn't want to follow my freaking instructions can you guys maybe show me where I'm going wrong and give me a short source code on how to rectify it. Code is below:
function validate(theForm)
{
var falseCounter = 0;
[code]....
View 7 Replies
View Related
May 3, 2010
I'm trying to validate the form, but each time I try to do so, all fields return invalid. I've tested my regular expressions with various online testers and they seem to work fine, am I perhaps using it in the wrong way? Or maybe a problem with my functions?
[Code]...
View 4 Replies
View Related
Mar 4, 2011
i'm trying to use a regular expression to verify an order. I basically don't want any decimals or negative numbers allowed. I believe I have any digit 0-9 one or more times and negating decimals. but for some reason it allows decimals and letters. also is it correct to put return true or can i just leave it blank?
var varifyThree = /^\d*[^\.]$/;
var productThree = document.getElementById('prod3').value;
if (productThree==null || productThree=="" || varifyThree.test(productThree)) {
return true;
[Code].....
View 8 Replies
View Related
Mar 7, 2011
I keep getting an error that says item is null. I'm a little lost because once I got my regExp to start working this actually worked for a little bit. When I came back I was getting an error.
var creditExp = /^([345])(\d{3})\-?(\d{4})\-?(\d{4})\-?(\d{4})$/;
var creditNumber = document.getElementById("creditnum").value;
var item = creditExp.exec(creditNumber);
document.getElementById("creditnum").value = item[1] + item[2] + "-" + item[3] + "-" + item[4] + "-" + item[5];
if (item[1] == 4 && document.getElementById("card1").checked == true) {
return true;
}else
if (item[1] == 5 && document.getElementById("card2").checked == true) {
return true;
}else
if (item[1] == 3 && document.getElementById("card3").checked == true) {
return true;
} else {
alert("You must enter a valid credit card number.");
document.getElementById("creditnum").focus();
return false;
}
var billingName = document.getElementById('creditname').value;
if (billingName == null || billingName == "") {
alert("You must enter your first name as printed on your credit card!");
document.getElementById('creditname').focus();
return false;
}
View 1 Replies
View Related
Feb 3, 2010
Code JavaScript:
function regExpression(stringValue) {
if (stringValue === null || stringValue.length === 0) {
alert("No string was entered"); }
else if (stringValue.match(/^[_.-a-z0-9]+@[_.-a-z]+.[a-z]{2,4}$/i) != null)
alert("This is a valid email.");
else {
alert("The string is not a valid email."); }
}
The problem is that: Code: john.smith@gmail123.com works. The 123 in the provider shouldn't be allowed, but it is. I'm trying to build a regular expressions for email.
View 2 Replies
View Related
Dec 27, 2005
Here's what used up the last couple of hours of my time.
The Mac version of MSIE will not load a script that has this in it:
theItem.elm.value = theItem.elm.value.replace(/^s*(.*?)s*$/, "$1");
Now I had other regular expressions in the script done this way, but in this one case I had to use the object constructor method.
It will won't work in this browser, but now at least the script loads and other functions in the same file work.
I have a solution so basically I'm looking for some insight into this if anyone has it. What is it about this particular expression that upsets Mac MSIE?
View 1 Replies
View Related
Dec 20, 2010
Explain to me why this doesn't work? code... All it does is crashes my browser.
View 1 Replies
View Related
Oct 4, 2011
I am trying to validate a date by using regular expressions.I have parts working, such as only accepts numbers, but I cannot get the range correct. On the "mm" field, I am getting errors.
if(form.mm.value=="")
{
alert("Please insert your birth month");
return false;}
[code]....
View 1 Replies
View Related
May 24, 2010
I'm trying to replace a ^ if the user types it in. This works ..
[Code]...
View 2 Replies
View Related