Rajthilak
Rajthilak

Reputation: 7

JavaScript Replace Method for multiple textboxes

Can anyone please tell me how I can use the replace method to replace a character if it occures in more than one textbox without having to write separate function for each textbox.

The code below is the basic way to use the replace method but it only allows for one textbox.

I'm sure I need a loop in there but I'm not sure how to use that without affecting the replace method.

<html>
<head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

    <script language="javascript">
        function stringReplace(form) {
            var replaceStr = form.textfield1.value
            var pattern = /\'/g;
            form.textfield1.value = replaceStr.replace(pattern, "''");
        }
    </script>
</head>
<body>
    <form name="form1" method="post" action="JStest_redirect.asp">
        <p>fname:
            <input type="text" name="textfield1" size="20">
        </p>
        <p>lname:
            <input type="text" name="textfield2" size="20">
        </p>
        <p>
            <input onclick="return stringReplace(form)" type="submit" name="Submit" value="Submit">
        </p>

    </form>
</body>
</html>

Upvotes: 1

Views: 285

Answers (5)

bastos.sergio
bastos.sergio

Reputation: 6764

If you wish to do it without jquery, you can use the getElementsByTagName() method.

function stringReplace(form) {

    var pattern = /\'/g;
    var inputs = form.getElementsByTagName("input");

    var input;
    for (var i = 0; i < inputs.length; i++) {
        input = inputs[i];
        if (input.type = 'text') {
            var replaceStr = input.value;
            input.value = replaceStr.replace(pattern, "''");

        }
    }

    return false;
}

Upvotes: 1

Ondřej Severa
Ondřej Severa

Reputation: 379

You can add onchange function to all of you text input

function stringReplace(textField) {
            var replaceStr = this.value
            var pattern = /\'/g;
            this.value = replaceStr.replace(pattern, "''");
        }

and than you add

<input type="text" name="textfield1" size="20" onchange="stringReplace(this);">

Upvotes: 0

kostas ch.
kostas ch.

Reputation: 2035

I believe that fisrt you have to take all the values

 function GetValue(){
var Contain = "";
$("#form1 :text").each(function(){
 //add replace code here
});

}

Upvotes: 0

palaѕн
palaѕн

Reputation: 73896

You can do this:

function stringReplace(form) {
    var $inputs = $(form).find('input:text');
    var pattern = /\'/g;
    $inputs.each(function () {
        this.value = this.value.replace(pattern, "''");
    });
    return false;  // Prevent the form from being submitted
}

This would find all the input type text within the form and replace their values.

Upvotes: 2

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382102

You seem to want

function stringReplace(form) {
   $('input[type=text]').val(function(_, v) {
      return v.replace(/\'/g, "''");
   });
   return false;
}

I added a return false at the end to prevent the form to be submitted on click. You probably want to have a separate button in your case.

Upvotes: 0

Related Questions