开发者

how to check "contains" using jquery [duplicate]

开发者 https://www.devze.com 2022-12-19 21:32 出处:网络
This question already has answers here: How to check whether a string contains a substring in JavaScript?
This question already has answers here: How to check whether a string contains a substring in JavaScript? (3 answers) 开发者_如何学C Closed 9 years ago.

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


Try this:

var _dob = "4/10";
var _adob;
if (_dob.indexOf("/") >-1) {
    _adob = _dob.split("/");
} else {
    _adob - _dob.split("-");
}


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('[-/.]'));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号