MacNuttin
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" />
height: 18px" type="text" />
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');" />
<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');" />
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
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)" />
<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)" />
ASKER
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: <input id="Text8" style="font-weight: bold; text-transform: uppercase; width: 17px;
height: 18px" type="text" onchange="return alert_form(this,document.a ll.Text8)" /><br />
Another Way?
<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');" />
<SCRIPT LANGUAGE=javascript>
<!--
function alert_form(obj)
{
if (obj.value != 'N' && obj.value != 'S') alert('You Must EnterY or N');
}
//-->
</SCRIPT>
Check: <input id="Text8" style="font-weight: bold; text-transform: uppercase; width: 17px;
height: 18px" type="text" onchange="return alert_form(this,document.a
Another Way?
<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);" />
<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.
Hope it helps anyway.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 = '';
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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)"/>
<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 = "";
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.
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.
just my $0.02