473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to take either single digit or double digit in textbox

149 100+
hi gud mrng,
in my form....i need to take either single digit in textbox i.e., '1' or '12'..what i did is as follows

login.jsp

Expand|Select|Wrap|Line Numbers
  1. <td><font color="#800000" size=""><b>Alert-Msg-Time (in Hrs)</b></font></td>
  2. <td><input type="text" name="alertMsg" maxlength="6"></td>
  3. <td></td>
  4.  
login.js

Expand|Select|Wrap|Line Numbers
  1. if (document.frm.alertMsg.value == "")
  2.   {
  3.        alert("Enter Alert Msg Time");
  4.        document.frm.alertMsg.focus();
  5.        return (false);
  6.    }
  7.    var chkAlrtmsg = /^[0123456789]{2}$/;
  8.    if (chkAlrtmsg.test(document.frm.alertMsg.value))
  9.    {}
  10.    else
  11.    {
  12.        alert('Invalid \" Alert Msg Time \" Entry');
  13.        return false;
  14.    }
  15.  
here i used regExp for taking 2 digits...but i need is it should take either of them....can anyone help me out...

thanks,
madhu.
Aug 6 '10 #1
20 2975
Dormilich
8,658 Expert Mod 8TB
why did you define a size (textbox) of 6, when you only need one of 2?

PS.
Expand|Select|Wrap|Line Numbers
  1. /^\d{1,2}$/
Aug 6 '10 #2
madhuriks
149 100+
hi,
thanks..its working i removed the 'maxlength'...i had another problem with another label..called 'L-Units'....in this txtbox i need to enter 'decimal numbers' as well as it should not take letters nor -ve values...here is my code.

login.jsp

Expand|Select|Wrap|Line Numbers
  1. <td></td>
  2.   <td><font color="#800000" size=""><b>L-Units</b>&nbsp;&nbsp;</font></td>
  3.   <td><input type="text" name="localUnits" id="localUnits">&nbsp;&nbsp;&nbsp;&nbsp;</td>
  4. <td></td>
  5.  
login.js

Expand|Select|Wrap|Line Numbers
  1. if (document.frm.localUnits.value == "")
  2.     {
  3.         alert("Enter Local Units");
  4.         document.frm.localUnits.focus();
  5.         return false;
  6.     }
  7.     var chklocalUnits = "0123456789";
  8.     var checklocalUnitsStr = document.frm.localUnits.value;
  9.     var alllocalUnitsValid = true;
  10.     for (i = 0;  i < checklocalUnitsStr.length;  i++)
  11.     {
  12.         ch = checklocalUnitsStr.charAt(i);
  13.         for (j = 0;  j < chklocalUnits.length;  j++)
  14.             if (ch == chklocalUnits.charAt(j))
  15.                 break;
  16.         if (j == chklocalUnits.length)
  17.         {
  18.             alllocalUnitsValid = false;
  19.             break;
  20.         }
  21.     }
  22.     if (!alllocalUnitsValid)
  23.     {
  24.         alert("Enter only numeric in the \" Local Units \" field , No -ve's are allowed.");
  25.         document.frm.localUnits.focus();
  26.         return (false);
  27.     }
  28.     var localUnitsValid = document.getElementById('localUnits').value;
  29.     for(var i1 = 0; i1 < localUnitsValid.length; i1++)
  30.     {
  31.         currentCode = localUnitsValid.charCodeAt(i1);
  32.         if(currentCode!=45 && currentCode!=46 && currentCode<48 || currentCode>57)
  33.         {
  34.             alert('Only Numbers are allowed in Local Units Valid');
  35.             return false;
  36.         } 
  37.         if(localUnitsValid.indexOf('.')!=-1 && localUnitsValid.lastIndexOf('.')!=localUnitsValid.indexOf('.')){
  38.             alert('A number can only have one decimal point');
  39.             return false;
  40.         }
  41.     }
  42.  
wht i wrote in js it is not accepting 'Decimal numbers'...but it is accepting 'No -ve values to be entered and letters not to be entered'....can u suggest where i wnt wrong...
Aug 6 '10 #3
Dormilich
8,658 Expert Mod 8TB
what is a "-ve" value?
Aug 6 '10 #4
madhuriks
149 100+
'-ve' means 'negative' value....
Aug 6 '10 #5
madhuriks
149 100+
the txtbox should not take 'negative' values...whts wrng in my code...
Aug 6 '10 #6
Dormilich
8,658 Expert Mod 8TB
IMO, too complicated test logic. convert the value into a number (parseFloat()) and test for NaN (not a numerical input) and x < 0.
Aug 6 '10 #7
madhuriks
149 100+
i had deleted some part of js code...here the code is

Expand|Select|Wrap|Line Numbers
  1. var stdUnitsValid = document.getElementById('stdUnits').value;
  2.     for(var i2 = 0; i2 < stdUnitsValid.length; i2++)
  3.     {
  4.         currentCode = stdUnitsValid.charCodeAt(i2);
  5.         if(currentCode!=45 && currentCode!=46 && currentCode<48 || currentCode>57)
  6.         {
  7.             alert('Only Numbers are allowed in Local Units Valid');
  8.             return false;
  9.         }
  10.         if(stdUnitsValid.indexOf('.')!=-1 && stdUnitsValid.lastIndexOf('.')!=stdUnitsValid.indexOf('.')){
  11.             alert('A number can only have one decimal point');
  12.             return false;
  13.         }
  14.     }
Aug 6 '10 #8
Dormilich
8,658 Expert Mod 8TB
I am still of the opinion, that you think too complicated. convert the input in a number and test, whether it is negative.
Aug 6 '10 #9
madhuriks
149 100+
i tried what u suggested...the problem im facing is if i enter the negative no. it is not showing any alert..i used if condition but not wrkng...hw to check for that..i removed if condition ..my code is

Expand|Select|Wrap|Line Numbers
  1. if (document.frm.localUnits.value == "")
  2.     {
  3.         alert("Enter Local Units");
  4.         document.frm.localUnits.focus();
  5.         return false;
  6.     }
  7.  
  8.     var localUnitsValid = document.getElementById('localUnits').value;
  9.     for(var i2 = 0; i2 < localUnitsValid.length; i2++)
  10.     {
  11.         currentCode = localUnitsValid.charCodeAt(i2);
  12.         if(currentCode!=45 && currentCode!=46 && currentCode<48 || currentCode>57)
  13.         {
  14.             alert('Only Numbers are allowed in STD Units Valid');
  15.             return false;
  16.         }
  17.     }
  18.     localUnitsValid = Math.abs(localUnitsValid);
  19.     document.getElementById('localUnits').value = Math.abs(localUnitsValid)
Aug 6 '10 #10
Dormilich
8,658 Expert Mod 8TB
i tried what u suggested...
according to your posted code, you did not.
Aug 6 '10 #11
madhuriks
149 100+
sorry for that i used 'Math' function...and i got it...by submitting the form..'negative' values converts to postive value in my o/p..but i need alert msg...where shld i code..
Aug 6 '10 #12
madhuriks
149 100+
hi,
im getting alert msg...i came to knw where i kept wrng..here is my chngd code..
Expand|Select|Wrap|Line Numbers
  1. if (localUnitsValid != Math.abs(localUnitsValid))
  2.             {
  3.                 alert('entered -ve value');
  4.                 localUnitsValid = Math.abs(localUnitsValid);
  5.                 return false;
  6.             }
Aug 6 '10 #13
Dormilich
8,658 Expert Mod 8TB
what about if (localUnitsValid < 0) ?
Aug 6 '10 #14
madhuriks
149 100+
im entering the values in txtbox...if the txt box is left empty..i'll throw an alert...
Aug 6 '10 #15
Dormilich
8,658 Expert Mod 8TB
sounds sensible …
Aug 6 '10 #16
madhuriks
149 100+
that means...i coded as follows.

Expand|Select|Wrap|Line Numbers
  1. if (document.frm.localUnits.value <=0)
  2.     {
  3.         alert("Enter Local Units");
  4.         document.frm.localUnits.focus();
  5.         return false;
  6.     }
Aug 6 '10 #17
Dormilich
8,658 Expert Mod 8TB
not a good idea. if you have non-numerical input (aka letters) how does that compare to 0?
Aug 6 '10 #18
madhuriks
149 100+
k then suggest me how to do...
Aug 6 '10 #19
Dormilich
8,658 Expert Mod 8TB
I already did that in post #7.
Aug 6 '10 #20
madhuriks
149 100+
k..i'll try it..
Aug 6 '10 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: sinister | last post by:
The examples in the online manual all seem to use double quotes, e.g. at http://us3.php.net/preg_replace Why? (The behavior is different with single quotes, and presumably simpler to...
12
by: Joshua Beall | last post by:
Hi All, I have heard other people say that PHP can parse double quoted strings (e.g., "Hello, World") faster than it can parse single quoted strings (e.g., 'Hello, World'). This seems backwards...
9
by: Dynamo | last post by:
Hi, I am still confused as when to use single or double quotes. This works: echo "<td>" . $row . "</td>"; and this does not
12
by: Sybren Stuvel | last post by:
Hi there! I'm a beginning Python programmer, and I like it a lot already. Since my Sharp Zaurus has a fairly complete Python environment including PyQT, I thought it would be the perfect...
2
by: Diarmaid McGleenan | last post by:
Hi all. This isn't quite the same single/double-quote problem we see posted a multitude of times in this ng. Read on... I have a js function called ShowActiveLink which accepts a string...
6
by: Sakharam Phapale | last post by:
Hi All, How to capture Mouse Single click and mouse double click event on Commnad Button. I am doing as follows. Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As...
7
by: Tor Aadnevik | last post by:
Hi, I have a problem converting values from Single to double. eg. When the Single value 12.19 is converted to double, the result is 12.1899995803833. Anyone know how to avoid this? Regards...
6
by: R.Biloti | last post by:
Hi folks I wrote the naive program to show up the unit roundoff (machine precision) for single and double precision: #include <stdio.h> int main (void) { double x;
15
by: bill | last post by:
I am trying to write clean code but keep having trouble deciding when to quote an array index and when not to. sometimes when I quote an array index inside of double quotes I get an error about...
1
by: eaocomp | last post by:
How do I trigger a single or double-click events when a user clicks a column within the gridview. I want the event to trigger and open a pop-up page related to the selected column. There are no...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.