SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Addict Feudalism's Avatar
    Join Date
    Jun 2002
    Location
    Buenos Aires, Argentina
    Posts
    361
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question We speak 2,5, but in javascript

    Im currently using this code to track if some value is numeric or not.
    Code:
    if (isDigit(document.nueva.ancho.value) == false){

    Problem is that in my country, we speak of "2,5" rather than "2.5" to talk about decimals. In that example, "2,5" means two dollars and a half.

    If my customer entryes "2,5", the "is not numeric" error displays.

    How can I change that?

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Keep your formatted strings separate from your numbers.
    Calculate with numbers, display strings.

    var num= parseFloat(document.nueva.ancho.value.replace(/,/g,'.'));
    if(isNaN(num)) // not a number
    // otherwise do whatever math you need with the number
    // And when you are done, display the reverse-
    str= (num+'').replace(/\./g,',');

  3. #3
    SitePoint Addict Feudalism's Avatar
    Join Date
    Jun 2002
    Location
    Buenos Aires, Argentina
    Posts
    361
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That solution will create more problems when trying to calculate dinamically some field.value.

    Im currently searching a way to automatically change "," to "." in the field. I tought of using onchange. Is there some way to trigger a function even on a keystroke?

  4. #4
    SitePoint Addict Feudalism's Avatar
    Join Date
    Jun 2002
    Location
    Buenos Aires, Argentina
    Posts
    361
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just found this script, that disables the numbers in some input field. I think Im in track for changing "," to "." , how can i do it?


    Code:
                
    <html>
    <body>
    <script type="text/javascript">
    function noNumbers(e)
    {
    var keynum
    var keychar
    var numcheck 	if(window.event) // IE
    {
    keynum = e.keyCode
    }
    else if(e.which) // Netscape/Firefox/Opera
    {
    keynum = e.which
    }
    keychar = String.fromCharCode(keynum)
    numcheck = /\d/
    return !numcheck.test(keychar)
    }
    </script> 	<form>
    <input type="text" onkeypress="return noNumbers(event)" />
    </form>

  5. #5
    SitePoint Addict Feudalism's Avatar
    Join Date
    Jun 2002
    Location
    Buenos Aires, Argentina
    Posts
    361
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
     <script type="text/javascript">
            function cambiocomma(thisfield){
                document.nueva.thisfield.value = document.nueva.thisfield.value.replace(',','.');
            }
        </script>
    
    <input type=text name=ancho onkeypress="cambiocomma(ancho)">
    Now, the problem is that I cant uste "thisfield" as a changing value

  6. #6
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <input type=text name=ancho onkeyup="cambiocomma('ancho')">
    Code:
      
     <script type="text/javascript">
    function cambiocomma(thisfield){
    var el = document.nueva.elements[thisfield] ;
    el.value = el.value.replace(',','.');
    }
    </script>
    
    <form name="nueva">
    <input type=text name=ancho onkeyup="cambiocomma('ancho')">
    </form>
    http://www.mredkj.com/tutorials/validate2.html
    Last edited by muazzez; Jun 16, 2007 at 01:20.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •