0
$(this).attr('id') == 'zipcode' && $this.value()!=(3, 4, 5)

What I tried to do here was call up the text input field with an id of "zipcode" and say something along the lines of "if value of zipcode is not 3, 4, or 5 then... etc etc..." I tried many combinations including || but nothing has worked. I'll be listing all the possible zip codes and would require the shortest possible way to do it.

Much appreciated.

Full code:

function validateStep(step){ if(step == fieldsetCount) return;

var error = 1;
var hasError = false;
$('#formElem').children(':nth-child('+ parseInt(step) +')').find(':input.req:not(button)').each(function(){
    var $this       = $(this);
    var valueLength = jQuery.trim($this.val()).length;
var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; 

if(valueLength == "" || $(this).attr('id') =='email' && !emailPattern.test($this.val()) || $(this).attr('id') == 'zipcode' && $this.value()!=(3, 4, 5))   

{
        hasError = true;
        $this.css('background-color','#FFEDEF');
    }
    else
        $this.css('background-color','#fff');

});
  • 1
    possible duplicate of [How to shorten my conditional statements](http://stackoverflow.com/questions/18347033/how-to-shorten-my-conditional-statements) – Phil Sep 02 '13 at 05:54

5 Answers5

3

One way to do this would be to use indexOf:

var values = [1,2,3,4];
var value = parseInt($(this).val());
if(values.indexOf(value) == -1) {
    //dostuff
}
Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
  • Why `parseInt` around the `indexOf` call? – user2357112 Sep 02 '13 at 05:53
  • @user2357112 Sorry, it was supposed to be around the val call. – Asad Saeeduddin Sep 02 '13 at 05:54
  • 1
    Why over complicate things ? `[1, 2, 3].indexOf( parseInt(this.value) ) === -1` – iConnor Sep 02 '13 at 05:55
  • @Pinocchio That's exactly what's happening in the code anyway. I just moved things to separate statements for clarity because I managed to get the `parseInt` call at the wrong depth because of all the nesting. – Asad Saeeduddin Sep 02 '13 at 05:56
  • 1
    @Pinocchio "`this.value` is nothing like `$(this).val()`" ... Seriously? `val` is a jQuery method for collections that exposes `value` for most input elements and the selected value for select boxes. What's the difference? – Asad Saeeduddin Sep 02 '13 at 06:00
  • @Pinocchio I wouldn't say *"nothing like"*. jQuery's `val` function does proxy to `elem.value` with some additional magic thrown in. See https://github.com/jquery/jquery/blob/master/src/attributes/val.js#L21 – Phil Sep 02 '13 at 06:01
  • Thanks for the help guys. I'm not good with scripting at all so I'm still trying to make sense of all this. Will get back to you guys on what works.... – user2738207 Sep 02 '13 at 06:15
0

This is a bit shorter:

$(this).attr('id') == 'zipcode' && $this.value() < 3 && $this.value() > 5

Vince
  • 1,517
  • 2
  • 18
  • 43
0
//
//  like this
//
function isnoteqto( value /* ...params*/ ) {
    return Array.prototype.slice.call( arguments, 1 ).every( function ( arg ) { return value !== arg; } );
}
//
public override
  • 974
  • 8
  • 17
0

I always like to extend the String prototype. Here is an example.

String.prototype.isNot = function() {
    for( var i = 0; i < arguments.length; i++ ) {
        if( this == arguments[i] ) return false;
    }
    return true;
};

Then you can do

var value = 'something';

if( value.isNot('value1', 'value2') ) // true

And

if( value.isNot('something') ) // false

If you don't like extending the String.prototype you can do this.

var isNot = function( value, args ) {
   for( var i = 0; i < args.length; i++ ) {
       if( value == args[i] ) return false;
   }
   return true;
}

And use like so.

var value = 'something';

if( isNot(value, ['value1', 'value2']) ) // true

And

if( isNot(value, ['something']) ) // false
iConnor
  • 19,997
  • 14
  • 62
  • 97
  • Extending native types for a tiny bit of syntax sugar is bad ettiquette IMO. edit: just saw your alternative. – Asad Saeeduddin Sep 02 '13 at 06:10
  • 1
    I like the syntactic sugar. for people that don't I have provided another option. I can also think of a lot more **Worse things** to be worrying about. – iConnor Sep 02 '13 at 06:11
-1
$(this).attr('id') == 'zipcode' && !/^(3|4|5)$/.test($(this).val())
David-SkyMesh
  • 5,041
  • 1
  • 31
  • 38
  • 1
    `/^[3-5]$/` would be even shorter here. – Vince Sep 02 '13 at 05:53
  • I think so far this is the one I'm able to understand.. Earlier I phrased a keyword wrong in my question. Not 3,4,5 IN zipcode, it should be if zipcode = 3,4 or 5. I've tried the other listed answers but some are just beyond my understanding :(. I get what your code is doing but how do I tweek it?...I really appreciate it. Thanks! – user2738207 Sep 02 '13 at 06:23
  • What exactly should it test for? If I've understood your re-wording, then this is already correct. The `^` and `$` cause it to anchor the regexp to the begining and end of the string. That way zipcode must be entirely either '3' or '4' or '5' (rather than just containing those characters). – David-SkyMesh Sep 02 '13 at 06:27
  • It's odd.. It's accepting it if the field has more than 2 of the listed value example: 34a, 44b, a34 – user2738207 Sep 02 '13 at 06:34
  • AH!!! Here we go: $(this).attr('id') == 'zipcode' && !/^(2|3|4|5)$/.test($(this).val()) Thanks! – user2738207 Sep 02 '13 at 06:43