Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

detect white spaces - javascript - textbox - regular expressions

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
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…
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

972 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