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

x
?
Solved

detect white spaces - javascript - textbox - regular expressions

Posted on 2006-07-10
5
Medium Priority
?
1,685 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 2000 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Suggested Courses

721 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