Something strange here. I successfully edited my post above then it reverted to unedited version without a refresh or anything BUT the edited version appeared again on qoting it! That's not good behaviour. I will report is as a bug.
This is my edited version. If it is identical to the post above, then please don't blame me - it's the site software.
With this version you can choose whether the test is case-sensitive or case-insensitive and it also returns a proper boolean (not -1 | index):
<script language="JavaScript" type="text/javascript">
//old English expression: "It's like trying to find a needle in a haystack"
function string_contains(haystack, needle, caseInsensitive){
if(needle.toString() === '') {
return false;
}
if(!caseInsensitive) {
return haystack.toString().indexOf(needle.toString()) >= 0;
}
else {
return haystack.toString().toLowerCase().indexOf(needle.toString().toLowerCase()) >= 0;
}
}
alert( string_contains('ABCDEF', 'N') );//false
alert( string_contains('ABCDEF', 'B') );//true
alert( string_contains('ABCDEF', 'b') );//false
alert( string_contains('ABCDEF', 'b', true) );//true
alert( string_contains('ABCDEF', '') );//false
</script>
caseInsensitive defaults to false (ie case-sensitive) if omitted.
if(needle.toString() === '') { return false; } ensures that an empty string does not test positive (which would otherwise be the case).
toString() guards against errors if non -strings are passed in.
I can't see why
theform.first_n.value should not work as along as
theform.first_n exists (and is within scope).
Airshow Last edited by Airshow; May 7th, 2009 at 11:00 pm.
Reputation Points: 318
Solved Threads: 358
WiFi Lounge Lizard
Offline 2,526 posts
since Apr 2009