1

I tried of checking contains option using jquery for the birthday but its getting exception

var _dob = "4/10";
// this line doesn't work
var _adob = _dob.contains('/') ? _dob.Split('/') : _dob.Split('-');
$('#Month').val(_adob[0]);
$('#Day').val(_adob[1]);

but i can't able to split.. its resulting in error on getting _adob itself

gnarf
  • 105,192
  • 25
  • 127
  • 161
kart
  • 625
  • 3
  • 12
  • 21

2 Answers2

1

Direct Answer

indexOf(something)>-1

var _dob = "4/10";
var _adob = _dob.indexOf('/')>-1 ? _dob.split('/') : _dob.split('-');
$('#Month').val(_adob[0]);
$('#Day').val(_adob[1]);

Indirectly

You really don't need to check that the string contains that... Using a regular expression, you can split on a -, /, or . by building a character set:

var _dob = '4.10';
var _aodb = _dob.split(new RegExp('[-/.]'));
gnarf
  • 105,192
  • 25
  • 127
  • 161
  • Split should be split (*it is case sensitive and the method is with lowercase s*) – Gabriele Petrioli Feb 13 '10 at 07:14
  • @Gaby - ty, corrected error in copy/paste section from OP... – gnarf Feb 13 '10 at 07:17
  • hi i got one more similar issue in the past...thing is i want to check the specific url from youtube alone and show success status and others i want to skip by stating it as not valid url var _videoUrl = "http://www.youtube.com/watch?v=FhnMNwiGg5M" if (_videoUrl.contains("youtube.com")')) { alert('Valid'); } else { alert('Not Valid'); } how to check with contains similar problem for me in that too... – kart Feb 13 '10 at 07:54
  • @unknown - `_videoUrl.indexOf('youtube.com')>-1` or `==0` to make sure its the first letters, or `_videoUrl.match(/^youtube\.com/)` for the regexp way. – gnarf Feb 13 '10 at 08:39
1

Try this:

var _dob = "4/10";
var _adob;
if (_dob.indexOf("/") >-1) {
    _adob = _dob.split("/");
} else {
    _adob - _dob.split("-");
}
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501