Solved

detect white spaces - javascript - textbox - regular expressions

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why "Mobile First"? 5 62
Passing client data from form to server 5 56
PHP Web Development 6 68
My tooltip is not displaying correct 11 16
Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
This video teaches users how to migrate an existing Wordpress website to a new domain.

734 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