It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

Help me.....onblur ()

Help me.....onblur ()

Postby hazrizal84 on Tue Jun 10, 2008 12:37 pm

hi..i really need help from those who can solve my prob..here there are:

<html>
<head>
<script language=javascript>
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 47 || charCode > 57))
return false;

return true;
}
</script>
</head>
<body>
<p>Please enter your answer or type ( / ) if you don't know the answer.<br>
Your answer may not contain both numbers and ( / ) sign.<br><br>
34 + 54 = <input type=text size="6" onkeypress="return isNumberKey(event)" onblur=""></p>
<p>123 + 76 = <input type=text size="6" name="T1" onkeypress="return isNumberKey(event)" onblur=""></p>
<input type=submit value="Check Answer!">
</body>
</html>

User will be able to key in their answer in numerical form or just type slash ( / ) for unknown answer..the prob is, how can i build up the function in onblur() statement so that, when tab button been pressed to move to the other textbox, it will determine whether user key in number, slash or the combination of number and slash. message box will be popped up if user key in both number and slash in the space provided.
hazrizal84
 
Posts: 22
Joined: Tue Jun 10, 2008 11:25 am

Postby rangana on Wed Jun 11, 2008 12:05 am

First of all, language is a deprecated attribute for script tag. Use type instead.

What will be the message if there was a number and slash combination? Will it throw an error?
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines

Postby hazrizal84 on Wed Jun 11, 2008 12:10 am

the message sort of, "Invalid answer entered. You may not answer both numbers and slash!!"...
help me plzz....i really need your help....
hazrizal84
 
Posts: 22
Joined: Tue Jun 10, 2008 11:25 am

Postby rangana on Wed Jun 11, 2008 12:39 am

I'm unaware of what you want to execute if "/" does'nt exist in the value. This might help:
Code: Select all
<html>
<head>
<script type="text/javascript">
window.onload=function()
{
   var inps=document.getElementsByTagName('input');
   for(var i=0;i<inps.length-1;i++)
      {
      inps[i].onblur=function()
         {
         if(this.value.indexOf('/'))
            {
            //What you want to execute here
            }
         else
         alert('You admit you don\'t know the answer!');
         }
      }
}
</script>
</head>
<body>
<p>Please enter your answer or type ( / ) if you don't know the answer.<br>
Your answer may not contain both numbers and ( / ) sign.<br><br>
34 + 54 = <input type=text size="6"></p>
<p>123 + 76 = <input type=text size="6" name="T1"></p>
<input type=submit value="Check Answer!">
</body>
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines

Postby hazrizal84 on Wed Jun 11, 2008 12:49 am

wow....!! what a wonderfull answer....thanx alot..i'll try it first..thanx... =)
hazrizal84
 
Posts: 22
Joined: Tue Jun 10, 2008 11:25 am

Postby rangana on Wed Jun 11, 2008 1:03 am

No problem, note that from the code i've given, you need not to assign onblur attribute in every input element. The script automatically loops through all the input box, except the last one which is for the submit.

If you're still stumped, get back ;)
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines

Postby hazrizal84 on Wed Jun 11, 2008 1:13 am

sir, may i have your yahoo messenger ID ?.. there is sumthing i need to discuss with u...
hazrizal84
 
Posts: 22
Joined: Tue Jun 10, 2008 11:25 am

Postby rangana on Wed Jun 11, 2008 1:17 am

What's it?...I rarely give my YM. I'm having fair amount of spams already.

It's not that I don't trust you, but I don't want to risk. If you tell me via PM what you want, or post it in public, then I might be able to weight it and give you my YM.
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines

Postby hazrizal84 on Wed Jun 11, 2008 1:24 am

oo..alrite...
bout the question i asked, i just allow the user to key in their answer in number or just let it be a slash for unknown answer.. i don want user to mix up number and slash in a given space, even they might be able to key in a slash. let's say, if a user try to key in the answer like : 43/21.. you see, there is a slash in the answer. so i don want that. when he/she key in 43/21 and press tab to move on to the next textbox, it will appear the error message box, state that, user has key in both number and slash.. u get my prob?.. looking 4ward for your answer.. thanx alot ..
hazrizal84
 
Posts: 22
Joined: Tue Jun 10, 2008 11:25 am

Postby rangana on Wed Jun 11, 2008 1:34 am

How about this modification instead:
Code: Select all
<script type="text/javascript">
window.onload=function()
{
   var inps=document.getElementsByTagName('input');
   for(var i=0;i<inps.length-1;i++)
      {
      inps[i].onblur=function()
         {
         if(this.value.indexOf('/')!=-1&&(this.value.length>1))
            {
         alert('Answers should either be number or slash only!\nNo Combination Please.');
            }
         }
      }
}
</script>
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines

Next

Who is online

Users browsing this forum: No registered users and 7 guests