[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

validating a text field!!!

Posted on 2002-07-15
11
Medium Priority
?
190 Views
Last Modified: 2008-03-03
hai all,

I have textbox for user to key in product_id, I wanted to check the following while keyin itself(it is a alphanumeric value)
1)first two characters should be alphabets.
2)remaining should b numeric.
3)user should not key in beyond 13 characters.

how am I  go about it? can any one feed back to me.

0
Comment
Question by:manju70
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
11 Comments
 
LVL 10

Expert Comment

by:zvonko
ID: 7156531
How about this:
<html>
<head>
<script>
function checkField(event, thisField) {
  thischar = (event.which) ? event.which : event.keyCode;
  if(thischar < 32) return true;
  if(thischar > 90) return false;
  if(thischar < 48) return false;
  if(thisField.value.length < 2) {
      if(thischar > 64) return true;
      alert('Only A-Z in first two positions.')
      return false;
  }
 return true;
}
</script>
</head>
<body>
<form>
<input type=text size=13 maxlength=13 onKeyDown="return checkField(event, this)">
</form>
</body>
</html>


Good luck,
zvonko

0
 
LVL 22

Expert Comment

by:CJ_S
ID: 7156572
Another one which also works in Netscape:

function checktextbox(tObj)
{
     sVal = tObj.value;
     sTmp = "";
     sAlpha = "abcdefghijklmnopqrstuvwxyz";
     sNumeric = "1234567890";
     
     bFalse = false;
     for(i=0;i<sVal.length;i++)
     {
          if(i<2 && sAlpha.indexOf(sVal.charAt(i))<0)
               bFalse = true;
          else
               sTmp += sVal.charAt(i);
          if(i>=2 && sNumeric.indexOf(sVal.charAt(i))<0)
               bFalse = true;
          else
               sTmp += sVal.charAt(i);
     }
     tObj.value = sTmp;
}

with:

<input type=text onKeyUp="checktextbox(this)" maxlength=13>
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 7156573
Can be shorter:

function checktextbox(tObj)
{
    sVal = tObj.value;
    sTmp = "";
    sAlpha = "abcdefghijklmnopqrstuvwxyz";
    sNumeric = "1234567890";
   
    bFalse = false;
    for(i=0;i<sVal.length;i++)
    {
         if(i<2 && sAlpha.indexOf(sVal.charAt(i))>=0)
              sTmp += sVal.charAt(i);
         if(i>=2 && sNumeric.indexOf(sVal.charAt(i))>=0)
              sTmp += sVal.charAt(i);
    }
    tObj.value = sTmp;
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 22

Expert Comment

by:CJ_S
ID: 7156574
and this one can be gone:
bFalse = false;
0
 

Author Comment

by:manju70
ID: 7156584
hai zvonko,
Thank you for ur reply

can u please give me the feed back as for what u check the first 3 if condititons.

actually I tried ur coding , it doesn't accepts key in numbers for first two characters. but can accept keyin alpabets for remaining 11 characters.(actually I require remaining 11 should accept only alphabets)

expecting ur reply.
manju
0
 

Author Comment

by:manju70
ID: 7156591
sorry remaining 11 characters should accept only numbers.

manju
0
 

Author Comment

by:manju70
ID: 7156614
sorry remaining 11 characters should accept only numbers.

manju
0
 
LVL 10

Accepted Solution

by:
zvonko earned 200 total points
ID: 7156718
Than this would do:
<html>
<head>
<script>
function checkField(event, thisField) {
 thischar = (event.which) ? event.which : event.keyCode;
//alert("key: "+thischar)
 if(thischar < 32) return true;
 if(thischar > 122) return false;
 if(thischar < 48) return false;
 if(thisField.value.length < 2) {
    if(thischar > 57) return true;
    alert('Only A-Z in first two positions.')
    return false;
 } else {
    if(thischar < 58) return true;
    alert('Only 0-1 after first two positions.')
    return false;
 }
}
</script>
</head>
<body>
<form>
<input type=text size=13 maxlength=13 onKeyDown="return checkField(event, this)">
</form>
</body>
</html>

This numbers are ASCII values of pressed key.
The first IF let all control keys pass, like backspace.
Numbers 48 to 57 are digits.
Numbers 58 to 122 are alpha.

You see?

0
 
LVL 10

Expert Comment

by:zvonko
ID: 7156725
Second active alert is a nonsense :)

It should read:
alert('Only 0-9 after first two positions.')

The first commented alert is to test key values you like to add to checking.

Cheers,
zvonko

0
 
LVL 10

Expert Comment

by:zvonko
ID: 7161645
Thanks for the points manju70 :)

How does my code work for you?

0
 

Author Comment

by:manju70
ID: 7163845
yah it works the way I expected.Thanks & hats off.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question