Solved

detect white spaces - javascript - textbox - regular expressions

Posted on 2006-07-10
5
1,679 Views
Last Modified: 2008-01-09
EE Team,

I am having problem validating the html textbox using javascript.

My textbox should only allow numbers, decimal numbers.

No special character or white spaces.

My code is working fine for all scenario's except for input starting with one or more white spaces or ending with one or more white spaces.

function Validate_Tarea(txtboxarea){
                    if (/^ *$/.test(txtboxarea.value)){
                    return false;}
                    else{return true;}}

When i tried to add this - doesnt work. for white spaces.

                    else if(/\s/.test(txtboxarea.value)){
                    return false;}
Please help.

Thanks
0
Comment
Question by:Apu_Shah
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:v_karthik
ID: 17076605
I'm trying with this -

/^\d+$/.test("  123")

There is a space in the beginning, and the expression returns false. If you remove it, it returns true.
\d stands for a digit. \d+ means one or more digits.
0
 
LVL 44

Accepted Solution

by:
scrathcyboy earned 500 total points
ID: 17079103
Here is one way --

<HTML>
   <HEAD>
   <SCRIPT language=Javascript>
      <!--
      function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
         return true;
      }
      //-->
   </SCRIPT>
   </HEAD>
   <BODY>
      <INPUT id="txtChar" onkeypress="return isNumberKey(event)"
         type="text" name="txtChar">
   </BODY>
</HTML>

--------

Here is a simpler way, only numbers --
var validNumber= /^\\d+(\\.\\d+)?$/

--------------

Here is numbers and one decimal point --

function allow_numeric(obj){
  if (!/^\d+(\.\d*)?$/.test(obj.value)){
    obj.value= theVal = obj.value.replace(/[^0-9.]/g,'');
    if(theVal>""){
      obj.value=parseFloat("0"+theVal);
      obj.focus()
    }
  }
}
----------------------

here is an input method --

<input type="text" name="NUMONLY" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')"/>

--------------------

Here is one for positive numbers no decimal points --

function check(str) {
  var re=/^\d+$/;// This is the regular expression
  if (re.test(str)) {
    alert("Good Number");
  } else {
    alert("Bad Entry!!");
  }
}
--------------------------
0
 
LVL 2

Author Comment

by:Apu_Shah
ID: 17081618
v_karthik your option fails when user enters only white spaces in the textbox.

scrathcyboy - I am only looking for detecting white spaces - thru regular expression.

Your option : !/^\d+(\.\d*)?$/.test(obj.value)) did not work either.

Please help..
0
 
LVL 2

Author Comment

by:Apu_Shah
ID: 17081941
Sorry scrathcyboy

This option did worked.

!/^\d+(\.\d*)?$/.test(obj.value)

Thanks
Closing.
0
 
LVL 44

Expert Comment

by:scrathcyboy
ID: 17085271
you have to watch those double parentensis -- glad you got it.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Read about why website design really matters in today's demanding market.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

856 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