0

I have some validation at the moment that returns false if a user enters a value that is less than 7 characters as so:

        if (nextPgeNum == 3 && $('#txt-centre').val().length < 7 ) {

            alert("Invalid Cost Centre");

            return false;

        }

What I need to do is add further validation to the same element to return false if #txt-centre doesn't begin with '212'

nsilva
  • 5,184
  • 16
  • 66
  • 108
  • You can refer http://stackoverflow.com/questions/8073510/jquery-check-if-string-starts-with-1234 – DimoMohit Sep 29 '14 at 12:48
  • Is it enough for the value to start with `212`? Or must *all* conditons be true at the same time? –  Sep 29 '14 at 12:48
  • possible duplicate of [How to check if a string "StartsWith" another string?](http://stackoverflow.com/questions/646628/how-to-check-if-a-string-startswith-another-string) – Logan Murphy Sep 29 '14 at 12:49
  • both length must be => 7 and value must start with '212' – nsilva Sep 29 '14 at 12:56

4 Answers4

3

I might go with a regex like

if (nextPgeNum == 3 && !/^212.{4,}/.test($('#txt-centre').val())) {
    alert("Invalid Cost Centre");
    return false;
}
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
2

I'd suggest, for simplicity, using indexOf():

if (nextPgeNum == 3 && $('#txt-centre').val().length < 7 &&  $('#txt-centre').val().indexOf('212') === 0) {

    alert("Invalid Cost Centre");

    return false;

}

It might be worth caching the value, since it's being accessed twice, and also removing leading/trailing white-space (with $.trim() or String.prototype.trim()):

var value = $.trim($('#txt-centre').val());
if (nextPgeNum == 3 && value.length < 7 &&  value.indexOf('212') === 0) {

    alert("Invalid Cost Centre");

    return false;

}
David Thomas
  • 249,100
  • 51
  • 377
  • 410
1

Try this:

if (nextPgeNum == 3 && $('#txt-centre').val().length < 7 && !$('#txt-centre').val().startsWith("212")) {

That is basic JavaScript.

  • 1
    Didnt know about that function, but the support isn't great... Well thank you for teaching me something ! – Karl-André Gagnon Sep 29 '14 at 12:47
  • "The support isn't great". What do you mean by that? –  Sep 29 '14 at 12:49
  • 2
    Limited compatibility for [`String.prototype.startsWith()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith). – David Thomas Sep 29 '14 at 12:50
  • That it is not supported by any IE, Safarie and opera : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith#Browser_compatibility – Karl-André Gagnon Sep 29 '14 at 12:51
  • They mean a lot of engines (including V8, Chrome's engine) don't have it, as it's not in the standard yet (not even ES5). ES6 will have it, and hopefully implementations will add it fairly quickly, but... – T.J. Crowder Sep 29 '14 at 12:51
  • Ah, I see. Thanks for the pointer. I just open Firebug and see what the autocompletion delivers :) –  Sep 29 '14 at 12:51
0

The most straightforward solution with good browser support is substring:

var val = $('#txt-centre').val();
if (nextPgeNum == 3 && val.length < 7 && val.substring(0, 3) !== "212") {

    alert("Invalid Cost Centre");

    return false;
}

I think you wanted !== there...

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875