Solved

detect white spaces - javascript - textbox - regular expressions

Posted on 2006-07-10
5
1,674 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now