User | Post |
Ramsus
Posts: 142/162 |
Originally posted by BGNG The only things that are restrictive, Ramsus, is that 1) the "this.value" only works if the statement is triggered by the text input's events and 2) the string to check is the numeric value of 12345. What if we want to click a button and check for "zyxwv"?
1. Change this.value to a reference to the value of the field you want to check and call it when you want the value checked. It's not brain surgery, and anyone who knows Javascript can do it in 10 seconds with a decent DOM reference.
<script type="text/javascript" language="javascript"> <!-- function checkInput(form) { if (form.exampleInput.value.match(/^\s*(12345)\s*/$)) form.exampleInput.value = ''; return false; } // --> </script>
<form onsubmit="return checkInput(this);" action="" method="GET"> <input type="text" name="exampleInput"> <input type="submit"> </form>
From the original post though, it sounded as though he wanted to check the input after the user changed it, not after pushing a button.
2. Did you even look at the second example?
<input type="text" onchange="if (this.value.match(/^\s*(zyxwv)\s*/$)) this.value='';">
Javascript has had regular expressions for a long time. Works in Mozilla, Safari, and IE mac, and is well documented as working in IE4 by MSDN.
EDIT: As a form with a button:
<script type="text/javascript" language="javascript"> <!-- function checkInput(form) { if (form.exampleInput.value.match(/^\s*(zyxwv)\s*/$)) form.exampleInput.value = ''; return false; } // --> </script>
<form onsubmit="return checkInput(this);" action="" method="GET"> <input type="text" name="exampleInput"> <input type="submit"> </form>
This also has the added benefit of simply running checkInput() on the form if the user hits enter. If you want to actually submit the form, change checkInput so it returns true. It'll check the input with checkInput, and then submit the form. |
SyntaxLegend
Posts: 203/222 |
I want it to be able to do it for alphanumeric, the "12345" was just an example. |
BGNG
Posts: 146/276 |
The only things that are restrictive, Ramsus, is that 1) the "this.value" only works if the statement is triggered by the text input's events and 2) the string to check is the numeric value of 12345. What if we want to click a button and check for "zyxwv"? |
Ramsus
Posts: 141/162 |
<input type="text" onchange="if (parseInt(this.value) == 12345) this.value='';">
and for strings:
<input type="text" onchange="if (this.value.match(/^\s*(12345)\s*/)) this.value='';"> |
BGNG
Posts: 142/276 |
Okay... I've written a function for trimming off spaces and tabs from the beginning and end of strings. This HTML file will do what you want:
<HTML><Head>
<Script Language="JavaScript">
function Trim(In) { var T = In; var Tab = "\u0009"; var Space = " "; while (T.charAt(0) == Tab || T.charAt(0) == Space) T = T.substring(1, T.length); while (T.charAt(T.length - 1) == Tab || T.charAt(T.length - 1) == Space) T = T.substring(0, T.length - 1); return T; }
</Script>
</Head><Body>
<Form> <Input Type="Text" Name="textbox"><BR> <Input Type="Button" Value="Click!" onClick="if (Trim(this.form.textbox.value) == '12345') this.form.textbox.value = '';"> </Form>
</Body></HTML> |
neotransotaku
Posts: 3566/4016 |
you may need some whitespace character trimming because i'm sure you know <space>12345 is not the same as 12345 |
BGNG
Posts: 141/276 |
Yup. You can do that with a simple comparison check:
<HTMl><Body> <Form> <Input Type="Text" Name="textbox"><BR> <Input Type="Button" Value="Click!" onClick="if (this.form.textbox.value == '12345') this.form.textbox.value = '';"> </form> </body></html>
|
Narf
Posts: 71/100 |
onclick="if(this.value=='12345'){this.value=' ';}" |
SyntaxLegend
Posts: 202/222 |
With javascript, is there anyway to make a textbox's value dissapear when it is a certain value. Like if the value of the textbox is "12345", only when the textbox is "12345" it sets it so there is nothing in the textbox, but if the textbox is "123456" it leaves it. Anyway to do that? |