Tomer Oszlak
Tomer Oszlak

Reputation: 225

getting string length in JS

This is my html code the code! But the JS is not working I tried a few things but i cant get the popup window if it less than 2

<script>
    function Check(name) {
        if (name.length < 2)
           alert("פחות משני תווים בשם");
    }
</script>

<input 
  id="Text1" 
  name="Text1" 
  type="text" 
  value="שם פרטי" 
  onmouseover="value=''" 
  onclick="Check();" 
/>

Upvotes: 1

Views: 232

Answers (4)

Alessandro Cabutto
Alessandro Cabutto

Reputation: 164

You should pass a parameter to your function:

<input id="Text1" name="Text1" type="text" value="שם פרטי" onmouseover="value=''" onclick="Check(this.value);" />

Upvotes: 0

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107508

You haven't quite hooked up your function correctly. You need to pass a value to it. In the context of the onclick event, this refers to the input element itself, and its value can be retrieved with this.value. You need to pass that value to your Check() function:

<input id="Text1" name="Text1" type="text" value="שם פרטי" 
    onmouseover="this.value=''" onclick="Check(this.value);" />

Likewise, you should set the value to empty with this.value='value' as I have also adjusted in your code. I don't know what you're intention is with the mouseover event, but this combination of events will only work if the user's cursor never leaves the textbox, which will certainly be counter-intuitive to many users.

Upvotes: 1

Kanagaraj M
Kanagaraj M

Reputation: 966

You can pass check(this) then it will get value of that text field.

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172398

Try passing a parameter to your function:-

Check(this.value);

like

<input id="Text1" name="Text1" type="text" value="שם פרטי" onmouseover="value=''" onclick="Check(document.getElementById('Text1').value);" />

Upvotes: 0

Related Questions