Validate phone number in form - forms

I am trying to get my code to work for a form. I want it to validate a phone number. It want to continue to tell me the phone number is not correct even when it is.
Here's what I have. I took out the rest of the code that pertain to this part to simplify it. I am not sure if I have the code right but I am looking to have someone only be able to type number. Enter a 10 digit string of number and have it change to the following format (XXX) XXX-XXXX if this is possible.
<?php
include_once "contact-config.php";
$error_message = '';
if (!isset($_POST['submit'])) {
showForm();
} else { //form submitted
$error = 0;
if(!empty($_POST['phone'])) {
$phone[2] = clean_var($_POST['phone']);
if (!validPhone($phone[2])) {
$error = 1;
$phone[3] = 'color:#FF0000;';
$phone[4] = '<strong><span style="color:#FF0000;">Invalid Phone Number</span></strong>';
}
}
else {
$error = 1;
$phone[3] = 'color:#FF0000;';
}
<td class="quote_text" style="width:{$left_col_width}; text-align:right; vertical-align:top; padding:{$cell_padding}; {$phone[3]}"><span class='required'><b>* </b></span>{$phone[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="text" name="{$phone[1]}" value="{$phone[2]}" size="20" maxlength="11" id="{$phone[1]}" /> {$phone[4]}</td>
/* Phone Number Validation Function. */
function validPhone($phone)
{
$isValid = true;
if(array_key_exists('phone', $_POST))
{
if(!preg_match('/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/', $_POST['phone']))
{
$isValid = false;
}
}
return $isValid;
}
?>

I have a few comments about your original code although I have not gone into the debugging in depth.
There's a regex at http://php.net/manual/en/function.preg-match.php that is more complicated you might try. It's about halfway down the page. I tried your regex out at both http://regexpal.com/ and http://www.solmetra.com/scripts/regex/index.php ; it did not work at either place on phone numbers I used. The regex I pulled from the PHP reference page, as shown below, works.
/^(?:1(?:[. -])?)?(?:((?=\d{3})))?([2-9]\d{2})(?:(?<=(\d{3})))? ?(?:(?<=\d{3})[.-])?([2-9]\d{2})[. -]?(\d{4})(?: (?i:ext).? ?(\d{1,5}))?$/
You're calling the function with a parameter then not using the parameter in the function body, which strikes me as strange.
This is probably a duplicate topic of PHP: Validation of US Phone numbers which is pretty comprehensive.

Related

List.JS - Filter with comma separated values

I'm trying to create a dropdown to filter based on dates, but I would like to be able to comma separate the dates instead of initilizing each field as a separate filter, which would be slow when you account for hundreds of dates, and each list item having 30+ dates each.
I thought maybe list.js supports comma separated fields, but I can't find any obvious solution online.
Here is a simple codepen with the code working with a single field:
https://codepen.io/mauricekindermann/pen/QWyqzQL
This: <span class="stat filter_dates">1999,2099,2199</span>
Instead of this: <span class="stat filter_dates">1999</span>
Is this possible? Or do I need to initiate each date as a separate filter?
I didn't get any answers and couldn't find an obvious soultion to this. So the final result uses this method (JS script inside a PHP file)
<script>
var options = {
valueNames: [
'id',
<?
while($item= mysqli_fetch_array($query))
{
?>
'filter_<?=$item['id']?>',
<?
}
?>
]
};
$(document).ready(function()
{
if($('#filter').length > 0)
{
$('#filter').change(function ()
{
var selection = this.value;
<?
$i=0;
mysqli_data_seek($query, 0);
while($item= mysqli_fetch_array($query))
{
if($i==0){
$type='if';
} else {
$type='else if';
}
?>
<?=$type?>(selection == '<?=$item['id']?>')
{
userList.filter(function (item) {
return (item.values().<?='filter_'.$item['id']?> == selection);
});
}
<?
$i++;
}
if($i > 0){
?>
else {
userList.filter();
}
<?
}
?>
});
};
});
</script>

Googlesheets: I need to assign a script to textbox that sends an email to an email address located in another spreadsheet

I'm sure this will be a frustrating post for some and I'm very sorry. I don't have any coding background and this will be one of the first scripts I've ever attempted. Thank you again for the help!
What I need to accomplish is to assign a script to a text box that sends an email with the address located in another spreadsheet.
Here is the setup: Picture 1. This is the spreadsheet and tab where I want the email address to be sent from using the textbox (with the E) located to the right of the "In-Game Name"
Picture 1.) http://imgur.com/a/8nVzl
The "In-Game Name" is already being imported from a separate spreadsheet. I have the url for this separate spreadsheet located in a separate tab of the current spreadsheet as you can see by Picture 2.
Picture 2 (left-side) and picture 3 (right-side) because my reputation isn't 10: http://imgur.com/a/q2bsH
You can see the data I'm importing from the second spreadsheet in picture 3 ("in-game name" and "prestige". I do not want to have the email address imported in this manner however.
I would like a script that sends an email from the main spreadsheet and pulls the email address from the second using the spreadsheet key url in the "Player Key Input" tab.
I have a sample of code that tries to accomplish this is currently only serving as comedic relief to angst from this matter.
Once again, I'm sorry if this was a poorly written/explained question. I'm awful with coding. I really appreciate any input!
We were able to get this to work using this script:
/**
* This is a utility function that sends an email.
* It requires a To address, a Subject, and the message Body.
*/
function sendEmail(to, subject, body) {
// The Reply To address is the default address that appears when a user replies to the email.
// If not set to a bogus email, it probably defaults to the owner of the sheet!
var replyTo = "donotreply#mail.com"
// Send the email!
MailApp.sendEmail(to, replyTo, subject, body)
}
/**
* This function is an example of a boilerplate message that you can send.
* You could duplicate this function for other types of messages.
*/
function sendAllianceMessageToUser(row) {
var user = getUserInfoFromRow(row);
sendEmail(user.email, "MCOC Alert", user.name+",\n\nPlease log on to MCOC and check AQ/AW!");
}
/**
* This utility function will retrieve a user's info when supplied with a row number.
* The row number must match the rows on the "Roster/Prestige" sheet.
* This function will not need to change unless the structure of the sheets change.
*/
function getUserInfoFromRow(row) {
// Keep a reference to the current active sheet
//var sheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.openById('1McfUWHPCgh7XWtLgZX6mcnLs_o1MQprBOKbHiwNsVEo').getSheetByName('Roster/Prestige');
// Grab the sheet key in the row that was provided. (In this case, column L)
//var skey = sheet.getRange('L'+row).getValue();
var skey = sheet.getRange('L4').getValue();
Logger.log(skey);
// Open the remote sheet using the ID that was retrieved earlier.
var remote = SpreadsheetApp.openById(skey).getSheetByName('Sheet1');
// Grab the necessary cell values
var ign = remote.getRange('C2').getValue();
var email = remote.getRange('C4').getValue();
var user = new Object();
user.name = ign;
user.email = email;
user.sheet_key = skey;
return user;
}
/**
* These functions are generic image click handlers for each image.
* Each image will call a function based on which row it is placed over.
* MOVING IMAGES WILL BREAK THIS!
*/
function row3_clicked() { sendAllianceMessageToUser('3'); }
function row4_clicked() { sendAllianceMessageToUser('4'); }
function row5_clicked() { sendAllianceMessageToUser('5'); }
function row6_clicked() { sendAllianceMessageToUser('6'); }
function row7_clicked() { sendAllianceMessageToUser('7'); }
function row8_clicked() { sendAllianceMessageToUser('8'); }
function row9_clicked() { sendAllianceMessageToUser('9'); }
function row10_clicked() { sendAllianceMessageToUser('10'); }
function row11_clicked() { sendAllianceMessageToUser('11'); }
function row12_clicked() { sendAllianceMessageToUser('12'); }
function row13_clicked() { sendAllianceMessageToUser('13'); }
function row14_clicked() { sendAllianceMessageToUser('14'); }
function row15_clicked() { sendAllianceMessageToUser('15'); }
function row16_clicked() { sendAllianceMessageToUser('16'); }
function row17_clicked() { sendAllianceMessageToUser('17'); }
function row18_clicked() { sendAllianceMessageToUser('18'); }
function row19_clicked() { sendAllianceMessageToUser('19'); }
function row20_clicked() { sendAllianceMessageToUser('20'); }
function row21_clicked() { sendAllianceMessageToUser('21'); }
function row22_clicked() { sendAllianceMessageToUser('22'); }
function row23_clicked() { sendAllianceMessageToUser('23'); }
function row24_clicked() { sendAllianceMessageToUser('24'); }
function row25_clicked() { sendAllianceMessageToUser('25'); }
function row26_clicked() { sendAllianceMessageToUser('26'); }
function row27_clicked() { sendAllianceMessageToUser('27'); }
function row28_clicked() { sendAllianceMessageToUser('28'); }
function row29_clicked() { sendAllianceMessageToUser('29'); }
function row30_clicked() { sendAllianceMessageToUser('30'); }
function row31_clicked() { sendAllianceMessageToUser('31'); }
function row32_clicked() { sendAllianceMessageToUser('32'); }
function test() {
sendAllianceMessageToUser('4'); // Testing!
}
The question "To pull the email address from another spreadsheet" I would use a formula to have it copied over. Then assign a script to email the address. I am not sure how you would pull info from another sheet via script.
The below formula will pull the spreadsheet data that is in A1 to the cell you assign.
=IMPORTRANGE("YOURSHEETID","Sheet1!A1:A")

Typo3 Formhandler : How to validate at least one of two fields in typoscript

I'm using formhandler extension v1.6.2 and Typo3 v6.1.5.
User must upload a CV or fill in LinkedIn ID. If none of the fields are filled, an error should appear and the form should not be submitted.
So, I would like to add typoscript conditions but it doesn't work :
HTML
<input type="file" id="file" name="cv[file]">
<input type="text" size="20" id="linkedin" name="cv[linkedin]">
Typoscript
plugin.Tx_Formhandler.settings {
if {
1 {
conditions {
OR1 {
AND1 = file=
AND2 = linkedin=
}
isTrue {
validators.1.config.fieldConf.linkedin.errorCheck.1 = required
}
}
}
}
}
Thanks for any help !
As far as I understand, it should be enough to make the file required if the text field is empty, right? Then try this:
plugin.Tx_Formhandler.settings {
if {
1 {
conditions.OR1.AND1 = linkedin=
isTrue {
validators.1.config.fieldConf.file.errorCheck.1 = fileRequired
}
}
}
}
BTW: using the predef stuff is much cleaner and safer if you have multiple forms on your site.

Prevent form submission on wrong captcha value

Hi i used a basic captcha script as below . The problem i am facing is that , inspite of wrong captcha value, the form is submitted . I need to prevent the form from submission if the value of Captcha is entered wrong .
<?php
if(isset($_POST['submit']))
{
$hash = (!empty($_POST['hash'])) ? preg_replace('/[\W]/i', '', trim($_POST['hash'])) : ''; // Remove any non alphanumeric characters to prevent exploit attempts
$captchacode = (!empty($_POST['captchacode'])) ? preg_replace('/[\W]/i', '', trim($_POST['captchacode'])) : ''; // Remove any non alphanumeric characters to prevent exploit attempts
// function to check the submitted captcha
function captchaChars($hash)
{
// Generate a 32 character string by getting the MD5 hash of the servers name with the hash added to the end.
// Adding the servers name means outside sources cannot just work out the characters from the hash
$captchastr = strtolower(md5($_SERVER['SERVER_NAME'] . $hash));
$captchastr2 = '';
for($i = 0; $i <= 28; $i += 7)
{
$captchastr2 .= $captchastr[$i];
}
return $captchastr2;
}
if(!empty($captchacode))
{
if(strtolower($captchacode) == captchaChars($hash)) // We convert submitted characters to lower case then compare with the expected answer
{
echo '<h3>The submitted characters were correct</h3>';
}
else
{
echo '<h3>The submitted characters were WRONG!</h3>';
return true;
}
}
else
{
echo '<h3>You forgot to fill in the code!</h3>';
return true;
}
}
?>
I had Captcha up and running on my website and it did nothing to prevent spam. It has been compromised as a spam prevention mechanism I'm afraid.
Besides that, you need to return false if the captcha is incorrect and the form will not be submitted.

Drupal 6: Modifying uid of a submitted node

I have a situation where I want a set of users (employees) to be able to create a node, but to replace the uid (user ID) with that of the users profile currently displayed.
In other words, I have a block that that calls a form for a content type. If an employee (uid = 20) goes to a clients page (uid =105), and fills out the form, I want the uid associated with the form to be the client's(105), not the employee's.
I'm using arg(1) to grab the Client's uid - here is what I have..
<?php
function addSR_form_service_request_node_form_alter(&$form, $form_state) {
if (arg(0) == 'user' && is_numeric(arg(1))) {
$form['#submit'][] = 'addSR_submit_function';
}
}
function addSR_submit_function($form, $form_state) {
$account = user_load(arg(1));
$form_state['values']['uid'] = $account->uid;
$form_state['values']['name'] = $account->name;
}
?>
The form is loading in the block, but when submitted, is still showing the employee uid. I don't want to use hook_form_alter because I don't want to modify the actual form, because clients can fill out the form directly, in this case, I don't want to modify the form at all.
I'm also ashamed that I'm putting this in a block, but I couldn't think of a way to put this in a module, so any suggestions on that would also be appreciated...
To create your form in a block, you could use the formblock module. Especially if you are not used to use the Drupal API. Then all that's left if to add your own submit handler to the form. This is a piece of code that is run, when the form is submitted. You only want to do this on clients pages so you would do that using the hook_form_alter function.
/**
* Hooks are placed in your module and are named modulename_hookname().
* So if a made a module that I called pony (the folder would then be called
* pony and it would need a pony.info and pony.module file I would create this function
*/
function pony_form_service_request_node_form_alter(&$form, $form_state) {
// Only affect the form, if it is submitted on the client/id url
if (arg(0) == 'client' && is_numeric(arg(1))) {
$form['#submit'][] = 'pony_my_own_submit_function';
}
}
function pony_my_own_submit_function($form, &$form_state) {
$account = user_load(arg(1));
$form_state['values']['uid'] = $account->uid;
$form_state['values']['name'] = $account->name;
}
The idea behind this code, is to only alter the form when the condition is met - that it is submitted on a client page. I guessed that the arg(0) would be client so if it's something else you would need to change that of cause. We only need to add a submit function, since what we want is to change the values if the form has passed validation.
Then if that is the case our 2nd function is run, which does that actual alteration of the values.
PHP blocks are bad. You can put them in a module.
function hook_block($op, $delta = 0) {
// Fill in $op = 'list';
if ($op == 'view' && $delta = 'whatever') {
$account = user_load(arg(1));
$node = array('uid' => $account->uid, 'name' => $account->name, 'type' => 'service_request', 'language' => '', '_service_request_client' => $account->uid);
$output = drupal_get_form('service_request_node_form', $node);
// Return properly formatted array.
}
}
Additionally, you want a form_alter just to enforce the values. It's ugly but it works.
function hook_form_service_request_node_form_alter(&$form, $form_state) {
if (isset($form_state['node']['_service_request_client'])) {
$form['buttons']['submit']['#submit'] = array('yourmodule_node_form_submit', 'node_form_submit');
}
}
function yourmodule_node_form_submit($form, &$form_state) {
$account = user_load($form_state['node']['_service_request_cilent'])l
$form_state['values']['uid'] = $account->uid;
$form_state['values']['name'] = $account->name;
}