Hướng dẫn php random generator

and thanks for taking time to at least pity my lack of knowledge.

I have a wordpress installation [stay with me..] that has a plugin where you enter some information including an email, in order to add, in this instance a new booking [crm type thing]. This is done in the back end by admin. On some occasions, the client doesn't have an email address and as a hacky workaround i'd like the system to just generate a random email address in the box whenever it's left empty.

Much of the plugin is in Javascript and i think i've found the validation function where my snippet might need 'injecting':

[1] I'm fairly comfortable with PHP, and have written something that generates a random email [which will only be used internally]

//Random email address generator
function generateRandomString[$length = 10] {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen[$characters];
$randomString = '';
for [$i = 0; $i < $length; $i++] {
    $randomString .= $characters[rand[0, $charactersLength - 1]];
}
$randomString .= "@mysite.co.uk";
return $randomString;

}

[yes I know, there are a million neater ways to accomplish the above]

[2] I think i've found the bit of javascript that validates the email input and was just hoping this may be the chunk of code i'd need to look at in order to generate a random email, if nothing else was entered.

function sln_validateBooking[] {
var $ = jQuery;
$['.sln-invalid'].removeClass['sln-invalid'];
$['.sln-error'].remove[];
var hasErrors = false;

var toValidate = [
    '#_sln_booking_service_select'
];

var fields = $['#salon-step-date'].attr['data-required_user_fields'].split[','];
$.each[fields, function[k, val] {
    toValidate.push['#_sln_booking_' + val];
}];

$.each[toValidate, function [k, val] {
    if [val == '#_sln_booking_phone' && !$['[name=_sln_booking_createuser]'].is[':checked']] {
        return;
    } else if [val == '#_sln_booking_email'] {
        if [!$['[name=_sln_booking_createuser]'].is[':checked'] && !$[val].val[]] {
            return;
        } else if [!sln_validateEmail[$[val].val[]]] {
            $[val].addClass['sln-invalid'].parent[].append['
This field is not a valid email
']; if [!hasErrors] $[val].focus[]; hasErrors = true; } } else if [val == '#_sln_booking_service_select'] { if [!$['[name=_sln_booking\\[services\\]\\[\\]]'].size[]] { $[val].addClass['sln-invalid'].parent[].append['
This field is required
']; if [!hasErrors] $[val].focus[]; hasErrors = true; } } else if [!$[val].val[]] { $[val].addClass['sln-invalid'].parent[].append['
This field is required
']; if [!hasErrors] $[val].focus[]; hasErrors = true; } }]; return !hasErrors;

}

What would be great to know is a] if i should just shoot myself in the face and never go near a line of code again and also b] if there is a neat solution to this that I may forever be in your debt for solving.

UPDATE: I've decided it would probably be better to pass this randomly generated email address to the empty field via a metabox, and not to hack at the plugin's code [which would only get overwritten]..

However.. I get 'uncaught referenceError : rand is not defined at HTMLinputelement. title for both functions if i do something like this:





function rand[] { document.getElementById["txt_name"].value = generateRandStr[]; } function generateRandStr[] { var candidates = "ABCDEFGHIJKLMNOPQRSTUVWXY123456789"; var randomemail = "", rand; for [var i = 0; i < 1; i++] { for [var j = 0; j < 8; j++] { rand = Math.floor[Math.random[] * candidates.length]; randomemail += candidates.charAt[rand]; } randomemail += "@blahblah.co.uk"; } return randomemail; } //working function fn_copy[] { var temp = document.getElementById["txt_name"].value; if [temp != ""] { document.getElementById["_sln_booking_email"].value = temp; } else alert["Text is Empty"]; }

HELP!

Permalink

Cannot retrieve contributors at this time

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

Chủ Đề