Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace characters in field on form submit

For example, if the field value was "John Wayne" I would want it to be replaced with "John_Wayne"

I'm thinking I can accomplish this through jQuery, basic idea is below:

$('#searchform').submit(function() {

//take current field value
//replace characters in field
//replace field with new value

}); 

Any help is appreciated.

like image 705
Tim Avatar asked Dec 22 '22 04:12

Tim


1 Answers

You could use the overload of val that takes a function:

$("input:text").val(function (i, value) {
    /* Return the new value here. "value" is the old value of the input: */
    return value.replace(/\s+/g, "_");
});

(You'll probably want your selector to be more specific than input:text)

Example: http://jsfiddle.net/nTXse/

like image 87
Andrew Whitaker Avatar answered Dec 24 '22 02:12

Andrew Whitaker