Link to home
Start Free TrialLog in
Avatar of MacNuttin
MacNuttinFlag for United States of America

asked on

validate asp text box contents on change

I need a text box to onchange go to javascript validate if Y or N or blank ok if not Alert("You Must EnterY or N")  Here's my asp: <input id="Text9" style="font-weight: bold; text-transform: uppercase; width: 17px;
        height: 18px" type="text" />
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

rather than a TextBox to enter Y or N, i would prefer a set of Radio Buttons, which makes more sense to the form and for users to fill in.

just my $0.02
try this:

<input id="Text9" style="font-weight: bold; text-transform: uppercase; width: 17px; height: 18px" type="text" onchange="JavaScript: if (this.value != 'N' && this.value != 'S') alert('You Must EnterY or N');" />
Avatar of MacNuttin

ASKER

ryancys: I need what I need :)
abachmann: I did try it and wow how does it work? I need to edit it a bit so blanks are acceptable and then make it a function so I can call it from many textboxes
you may try this...

<script language="JavaScript">
      function YesNoOnly(obj) {
            if ((obj.value.toUpperCase() != "Y") && (obj.value.toUpperCase() != "N")) {
                  alert("You Must Enter Y or N");
                  obj.focus();
                  obj.select();
            }
      }
</script>

then:

<input id="Text9" style="font-weight: bold; text-transform: uppercase; width: 17px;height: 18px" type="text" onKeyUp="YesNoOnly(this)" />
Hey I did it look:

<SCRIPT LANGUAGE=javascript>
<!--
function alert_form(obj)
{
    if (obj.value != 'N' && obj.value != 'S') alert('You Must EnterY or N');
}
//-->
</SCRIPT>
  Check:&nbsp; <input id="Text8" style="font-weight: bold; text-transform: uppercase; width: 17px;
        height: 18px" type="text" onchange="return alert_form(this,document.all.Text8)"/><br />
    Another Way?&nbsp;
    <input id="Text9" style="font-weight: bold; text-transform: uppercase; width: 17px;
        height: 18px" type="text" onchange="JavaScript: if (this.value != 'N' && this.value != 'S') alert('You Must EnterY or N');" />
then you have to add a script block:

<script language="JavaScript">
function Validate (txt) {
    if (txt != 'S' && txt != 'N' && txt != '') //just add all the validations you need in there
          alert('Whatever you want to say...');
}
</script>

and then use this for the Input:

<input id="Text9" style="font-weight: bold; text-transform: uppercase; width: 17px; height: 18px" type="text" onchange="JavaScript: Validate(this.value);" />
Yes, this should work, sorry, I haven't read it before posting the last one.
Hope it helps anyway.
SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is great help guys. Can the txt.value be reset to blank if the alert is triggered?
you can, but not passing the "value" as the parameter.
it doesn't pass as a reference

you have to pass the object instead, calling the function "Validate(this)"
then in the function :

function Validate (txt) {
    if (txt.value != 'S' && txt.value != 'N' && txt.value != '') //just add all the validations you need in there
    {
          alert('Whatever you want to say...');
          this.value = '';
    }
}
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
trying to blank the value now...
<SCRIPT LANGUAGE=javascript>
<!--
function alert_form(txt)
{
     if (txt != 'Y' && txt != 'N' && txt != '')alert('You Must EnterY or N');txt !="";
}
</SCRIPT>
<input id="Text8" style="font-weight: bold; text-transform: uppercase; width: 17px;
        height: 18px" type="text" onchange="return alert_form(this.value)"/>
>>Can the txt.value be reset to blank if the alert is triggered?

for my version, try:

<script language="JavaScript">
      function YesNoOnly(obj) {
            if ((obj.value.toUpperCase() != "Y") && (obj.value.toUpperCase() != "N")) {
                  alert("You Must Enter Y or N");
                  obj.value = "";
                  obj.focus();
                  //obj.select();
            }
      }
</script>

then:

<input id="Text9" style="font-weight: bold; text-transform: uppercase; width: 17px;height: 18px" type="text" onKeyUp="YesNoOnly(this)" />



generally, you just need set it's Value Property to "", like:

txt.value = "";
no, you have to use the brackets if you want to put more than one sentence in an if statemen (or any other conditional construct)

if (txt != 'Y' && txt != 'N' && txt != '')alert('You Must EnterY or N');txt !="";
means:

if (txt != 'Y' && txt != 'N' && txt != '')
{
      alert('You Must EnterY or N');
}

txt !="";
and you would be blanking the value no matter if it is right or wrong.