Hello,
I have a little question.
Lets say I have string:
string g="ZOMGGGG";
and a Boolean var:
bool S;

so in C# if I write S=g.contains("GGGG");
and I get true if it contains else false.
How do I use that in JavaScript?
Thank You!

JC

you cant directly as they are two seperate languages. you could output content to the page using a script block somthign like the following

string g = "ZOMGGGG";
bool s = g.contains("GGGG");
Response.Write("<script type=\"text/javascript\">alert('" + s + "');</script>");

I mean in a form that i make in Html, i need to check that the user name name value doesn't contain the first name of the person.
so what You wrote instead of the 3rd line(replacing by) I can write return s; ? It's in a function...
so the ".contains" works on javascript?
awesome!
thanks :D

I mean in a form that i make in Html, i need to check that the user name name value doesn't contain the first name of the person.
so what You wrote instead of the 3rd line(replacing by) I can write return s; ? It's in a function...
so the ".contains" works on javascript?
awesome!
thanks :D

i think you mis interpreted my response then to check if a string contains a string in javascript you want the following.

function stringContains(string, value) {
    // To make this case insensitive comment out the following 2 lines
    // string = string.toLowerCase();
    // value = value.toLowerCase();
    return string.indexOf(value);
}

// Then call it with the following
var g = 'ZOMGGGG';
var s = stringContains(g, 'GGGG');

Bumping cause I couldn't get it to work, The function should return -1 if it doesn't contain the string right?
this is what I've done:

function string_contains(string,value)
{   string=string.toLowerCase();
    value=value.toLowerCase();
    return string.indexOf(value);
}

if (string_contains(theform.pass.value,theform.first_n.value)!=-1)
   {

      theform.pass.focus();
 
      theform.pass.select();
 
      newalert(iderror16);
 
      return (false);
 
   }

what's wrong?
cant i send the first_n as a value??

Hi JooClops,

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

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

Awesome, it works :P
thanks Airshow!
btw ""It's like trying to find a needle in a haystack"
I know this expression :)

thx again.
JC

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.