-1

I'm trying to validate some of the field in my form.

and I want to check if they have input the correct area code of the required field.

The area code should be one of 212, 313, or 414

This is homework so I can't use regular expression, and I'm not really asking for an answer but here is what I'm trying to use but non of them really worked for me

if (input.substr(0,2) != 212|| input.substr(0,2) != 313 || input.substr(0,2) != 414)
                Message += " <p>Sorry but you have enter wrong area code!!!</p>";

I've tried used substring indexOf but really I don't know non of them really correct in this case.

Is there anyway to validate but not with the regular expression?

Thanks

5
  • have you tried to make sure input has a value? try calling alert(input) and see what happens. Commented Apr 1, 2012 at 23:45
  • 1
    The main problem is that substring(0, 2) only gives you 2 characters. Not three. But you should also switch the comparisons to strings and use !== Commented Apr 1, 2012 at 23:52
  • @KshitijMehta Yes the value I get was correct and I can validate the rest of the code except to make sure if the area code is correct. Commented Apr 1, 2012 at 23:54
  • @AutoSponge I'm going to try with 3 if that was the case. Commented Apr 1, 2012 at 23:55
  • @AutoSponge thanks for pointing out that and that was one of the problem :D Commented Apr 2, 2012 at 0:04

3 Answers 3

1
<input type="text" id="num"/>
<div id="msg"></div>

// This should run in a window.onload or $(document).ready() block
var num = document.getElementById('num'),
    msg = document.getElementById('msg');

num.onblur = function(){
    var areacodes = '212|313|414|',
        areacode = this.value.replace(/[^0-9]/, '').substring(0,3),
        message = 'Area code validates.';

    if (areacode.length < 3 || areacodes.indexOf(areacode + '|') === -1) {
        message = "Sorry but you have entered an invalid area code.";
    }

    msg.innerHTML = message;
};

http://jsfiddle.net/userdude/gfJWX/1

input I'm guessing is a string variable, and not a DOM note pointing to an input element.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes input was just the name I assign from the field value, EDIT I tested and it doesn't work :(
I found out that the problem was (0,2) it suppose to be (0,3)
Yes, it should be three. I also added a normalizer for non-numeric characters: jsfiddle.net/userdude/gfJWX
How is this the answer when he said "This is homework so I can't use regular expression,"?
@LarryBattle - The validation itself has nothing to do with regular expression, I added that to demonstrate a point that (212) wouldn't validate, although many would enter this. I also noticed I had the regex at the wrong spot. Technically, remove it and it will still "work", just won't match all forms of an entered area code.
1

Try this

if (input.substr(0,3) != 212|| input.substr(0,3) != 313 || input.substr(0,3) != 414)
      Message += " <p>Sorry but you have enter wrong area code!!!</p>";

I didn't test it though

4 Comments

Why is the first argument of substr increasing? input can be one of 212, 313, or 414.
I'm guessing that your code will validate 313 and 414 in a wrong position the area code will only be from index 0-2
I thought the values should be entered at once, I've edited it.
@Chibuzo thanks but once I tried it still doesn't work I don't know why.
0

Shouldn't this work?

var str = input.trim().substr(0,3);
if ( !{212:1, 313:1, 414:1}[ str ] ){
    Message += " <p>Sorry but you have enter wrong area code!!!</p>";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.