Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    Replace di un campo con javascript

    Ciao ragazzi...
    volevo sapere se è possibile una cosa di questo genere...

    Ho un form in una pagina asp che al submit dello stesso richiama un javascript che fa determinati controlli sui campi...

    Io vorrei fare il replace dei campi numerici in questo modo:
    valore digitato = 2,46
    valore replace = 2.46
    ovvero rimpiazzare la virgola con il punto che per access significa virgola...

    Qualcuno mi può aiutare? :metallica

  2. #2
    Utente di HTML.it L'avatar di ilgiovo
    Registrato dal
    Aug 2001
    Messaggi
    518
    usa il metodo del'oggetto stringa "replace"

    replace
    Finds a match between a regular expression and a string, and replaces the matched substring with a new substring. Method of
    String

    Implemented in
    JavaScript 1.2


    JavaScript 1.3: supports the nesting of a function in place of the second argument



    Syntax
    replace(regexp, newSubStr)replace(regexp, function)
    Versions prior to JavaScript 1.3:

    replace(regexp, newSubStr)
    Parameters
    regexp
    The name of the regular expression. It can be a variable name or a literal.

    newSubStr
    The string to put in place of the string found with regexp. This string can include the RegExp properties $1, ..., $9, lastMatch, lastParen, leftContext, and rightContext.

    function
    A function to be invoked after the match has been performed.



    Description
    This method does not change the String object it is called on; it simply returns a new string.

    If you want to execute a global search and replace, or a case insensitive search, include the g (for global) and i (for ignore case) flags in the regular expression. These can be included separately or together. The following two examples below show how to use these flags with replace.

    Specifying a function as a parameter. When you specify a function as the second parameter, the function is invoked after the match has been performed. (The use of a function in this manner is often called a lambda expression.)

    In your function, you can dynamically generate the string that replaces the matched substring. The result of the function call is used as the replacement value.

    The nested function can use the matched substrings to determine the new string (newSubStr) that replaces the found substring. You get the matched substrings through the parameters of your function. The first parameter of your function holds the complete matched substring. Other parameters can be used for parenthetical matches, remembered submatch strings. For example, the following replace method returns XX.zzzz - XX , zzzz.

    "XXzzzz".replace(/(X*)(z*)/, function (str, p1, p2) { return str + " - " + p1 + " , " + p2; } )
    The array returned from the exec method of the RegExp object and the subsequent match is available to your function. You can use the content of the array plus the input and the index (index of match in the input string) properties of the array to perform additional tasks before the method replaces the substring.

    Examples
    Example 1. In the following example, the regular expression includes the global and ignore case flags which permits replace to replace each occurrence of 'apples' in the string with 'oranges.'

    <SCRIPT>re = /apples/gi;str = "Apples are round, and apples are juicy.";newstr=str.replace(re, "oranges");document.write(newstr)</SCRIPT>
    This prints "oranges are round, and oranges are juicy."

    Example 2. In the following example, the regular expression is defined in replace and includes the ignore case flag.

    <SCRIPT>str = "Twas the night before Xmas...";newstr=str.replace(/xmas/i, "Christmas");document.write(newstr)</SCRIPT>
    This prints "Twas the night before Christmas..."

    Example 3. The following script switches the words in the string. For the replacement text, the script uses the values of the $1 and $2 properties.

    <SCRIPT LANGUAGE="JavaScript1.2">re = /(\w+)\s(\w+)/;str = "John Smith";newstr = str.replace(re, "$2, $1");document.write(newstr)</SCRIPT>
    This prints "Smith, John".

    Example 4. The following example replaces a Fahrenheit degree with its equivalent Celsius degree. The Fahrenheit degree should be a number ending with F. The function returns the Celsius number ending with C. For example, if the input number is 212F, the function returns 100C. If the number is 0F, the function returns -17.77777777777778C.

    The regular expression test checks for any number that ends with F. The number of Fahrenheit degree is accessible to your function through the parameter $1. The function sets the Celsius number based on the Fahrenheit degree passed in a string to the f2c function. f2c then returns the Celsius number. This function approximates Perl's s///e flag.

    function f2c(x) { var s = String(x) var test = /(\d+(\.\d*)?)F\b/g return s.replace (test, myfunction ($0,$1,$2) { return (($1-32) * 5/9) + "C"; } )
    Cacca!

  3. #3
    Utente di HTML.it
    Registrato dal
    Sep 2001
    Messaggi
    21,188
    Nel controllo del form prima della spedizione devi inserire:

    var str = document.NOMEFORM.NOMECAMPO.value;
    str = str.replace(/\,/, ".");
    document.NOMEFORM.NOMECAMPO.value = str;

    Ciao
    Michele
    Nuova politica di maggiore severita` sui titoli delle discussioni: (ri)leggete il regolamento
    No domande tecniche in messaggi privati

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.