Skip to content

Instantly share code, notes, and snippets.

@RadGH
Last active April 14, 2023 16:31
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RadGH/6096145 to your computer and use it in GitHub Desktop.
Save RadGH/6096145 to your computer and use it in GitHub Desktop.
This javascript will fill all form fields with "dummy" (testing) information. Email fields will be appended @example.org. Phone number fields will generate a 10-digit number. Confirmation fields will match their original fields (assuming they have "confirm" in the name, eg "confirm-email"). Buttons and hidden inputs will be ignored. Click and ch…
function randomstr( length, digits ) {
if ( typeof length == 'undefined' ) length = 6;
if ( typeof digits == 'undefined' ) digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var str = '';
for( i = 0; i < length; i++ ) str += digits.charAt( Math.floor( Math.random() * digits.length ) );
return str;
}
jQuery('form input, form select, form textarea').each(function() {
var $input = jQuery(this);
if ( $input.is('input[type=button], input[type=submit], input[type=image], input[type=hidden]') ) return;
$input.val( randomstr(10) );
if ( $input.attr('name') && $input.attr('name').indexOf( 'phone' ) >= 0 ) {
$input.val( randomstr( 10, '0123456789' ) );
}
if ( $input.is('input[type=email]') ) $input.val( $input.val() + '@example.org' );
if ( $input.attr('name') && $input.attr('name').indexOf( 'confirm' ) >= 0 ) {
var $form = $input.parents('form');
if ( $form.length ) {
var basename = $input.attr('name').replace('-confirm', '').replace('confirm-', '').replace('_confirm', '').replace('confirm_', '').replace('confirm', '');
var $original = $form.find( '[name=' + basename + ']' );
if ( $original.length ) $input.val( $original.val() );
}
}
if ( $input.is('select') ) {
var $options = $input.find('option[value]');
var $random = $options.eq( Math.floor( Math.random() * $options.length ) );
$input.val( $random.val() );
}
if ( $input.is('input[type=text], input[type=email], input[type=password], textarea, input[type=checkbox], input[type=radio], select') )
$input.click().change();
});
@RadGH
Copy link
Author

RadGH commented Jul 27, 2013

This code can be bookmarked, allowing you to run the above code in the click of a button!

javascript: function randomstr( length, digits ) { if ( typeof length == 'undefined' ) length = 6; if ( typeof digits == 'undefined' ) digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; var str = ''; for( i = 0; i < length; i++ ) str += digits.charAt( Math.floor( Math.random() * digits.length ) ); return str; } jQuery('form input, form select, form textarea').each(function() { var $input = jQuery(this); if ( $input.is('input[type=button], input[type=submit], input[type=image], input[type=hidden]') ) return; $input.val( randomstr(10) ); if ( $input.attr('name') && $input.attr('name').indexOf( 'phone' ) >= 0 ) { $input.val( randomstr( 10, '0123456789' ) ); } if ( $input.is('input[type=email]') ) $input.val( $input.val() + '@example.org' ); if ( $input.attr('name') && $input.attr('name').indexOf( 'confirm' ) >= 0 ) { var $form = $input.parents('form'); if ( $form.length ) { var basename = $input.attr('name').replace('-confirm', '').replace('confirm-', '').replace('_confirm', '').replace('confirm_', '').replace('confirm', ''); var $original = $form.find( '[name=' + basename + ']' ); if ( $original.length ) $input.val( $original.val() ); } } if ( $input.is('select') ) { var $options = $input.find('option[value]'); var $random = $options.eq( Math.floor( Math.random() * $options.length ) ); $input.val( $random.val() ); } if ( $input.is('input[type=text], input[type=email], input[type=password], textarea, input[type=checkbox], input[type=radio], select') ) $input.click().change(); });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment