Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

check the fields only accept numeric

Posted on 1999-07-30
6
Medium Priority
?
240 Views
Last Modified: 2013-12-25
Hi,

How to check the fields that only can accept numeric type value? else display the error message to tell the user that he/she must type in numeric/number in that text box.

Thanks.
0
Comment
Question by:ht055
[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
6 Comments
 
LVL 3

Expert Comment

by:slok
ID: 1835275
what language are u using?
0
 
LVL 11

Accepted Solution

by:
mouatts earned 120 total points
ID: 1835276
Heres one I prepared earlier

<SCRIPT LANGUAGE="javascript">
var blur_lock=false;
function isNumeric( f )
{
      if(f == null)
      {
            return false;
      }
      for (var i=0;i <f.length;++i)
      {
            var c=f.charAt(i);
            if(c!='.' && (c<'0' || c>'9'))
            {
                  if(i>0 || c!='-')
                  {
                        return false;
                  }
            }
      }
      return true;
}
function isNumber(f, n, fo)
{
      if(typeof fo =='undefined')
      {
            fo=0;
      }
      var s=document.forms[fo] [f].value;
      if(s == null || s.length==0)
      {
            blur_lock=false;
            return true;
      }
      else
      {
            if(!isNumeric(s))
            {
                  if(!blur_lock)
                  {
                        confirm( n+" must be numeric");
                        blur_lock=true;
                  }
                  document.forms[fo] [f].focus();
                  return false;
            }
      }
      blur_lock=false;
      return true;
}
</SCRIPT>

I normally use this on the onBlur event of a field eg

<INPUT TYPE="TEXT" NAME="myfield" onBlur="isNumber('myfield', 'The blah blah');">

If you have multiple forms within the page add the form number as the third parameter. If the person fails to puts something non-numeric in a dialog will pop up and when the dismiss it willl be focused back to the field. (incidentally it allows negative numbers).

HTH
Steve

0
 

Author Comment

by:ht055
ID: 1835277
Hi,

How to use the CGI  to check it? I afraid some of the browser cannot support this Javascript.

Do you mind to show me on CGI platform?

Thank you.


0
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
LVL 11

Expert Comment

by:mouatts
ID: 1835278
By the way a slight mistake!
>I normally use this on the onBlur event of a field eg

><INPUT TYPE="TEXT" NAME="myfield" onBlur="isNumber('myfield', 'The blah blah');">

should read

I normally use this on the onFocus event of a field eg

<INPUT TYPE="TEXT" NAME="myfield" onFocus="isNumber('myfield', 'The blah blah');">

So far as CGI is concerned it becomes a different proposition in that the user will have to hit the submit button, the cgi will have to process the entire form and then return a brand new page to show which field(s) are in error. It can certainly be done but it will look distinctly different to the javascript solution (which will work on most browsers if Javascript is turned on). Which language would the CGI be in?

Steve


0
 

Author Comment

by:ht055
ID: 1835279
Hi Steve,

It should be in Perl 5.

I agree with you answer. Javascript is much better then CGI.

Just wanna to learn one more news things in CGI for form checking.

Thank you very much!

0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6872273
GREETINGS!

This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects the problem and the expert will now receive these points; points verified.

Please click on your Member Profile and select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.  If you are an EE Pro user, you can also choose Power Search to find all your open questions.

This is the Community Support link, if help is needed, along with the link to All Topics which reflects many TAs recently added.

http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
http://www.experts-exchange.com/jsp/zonesAll.jsp
 
Thank you,
Moondancer
Moderator @ Experts Exchange
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

670 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