// Added javascript validation functions (demo)

This commit is contained in:
Damien Metzger
2013-10-04 15:24:50 +02:00
parent 16dfa4e73d
commit 5c5a989cda
3 changed files with 98 additions and 15 deletions
+4 -2
View File
@@ -44,6 +44,7 @@ class AddressControllerCore extends FrontController
{
parent::setMedia();
$this->addJS(_THEME_JS_DIR_.'tools/statesManagement.js');
$this->addJS(_PS_JS_DIR_.'validate.js');
}
/**
@@ -268,6 +269,7 @@ class AddressControllerCore extends FrontController
// Assign common vars
$this->context->smarty->assign(array(
'address_validation' => Address::$definition['fields'],
'one_phone_at_least' => (int)Configuration::get('PS_ONE_PHONE_AT_LEAST'),
'onr_phone_at_least' => (int)Configuration::get('PS_ONE_PHONE_AT_LEAST'), //retro compat
'ajaxurl' => _MODULE_DIR_,
@@ -339,8 +341,8 @@ class AddressControllerCore extends FrontController
protected function assignAddressFormat()
{
$id_country = is_null($this->_address)? 0 : (int)$this->_address->id_country;
$dlv_adr_fields = AddressFormat::getOrderedAddressFields($id_country, true, true);
$this->context->smarty->assign('ordered_adr_fields', $dlv_adr_fields);
$ordered_adr_fields = AddressFormat::getOrderedAddressFields($id_country, true, true);
$this->context->smarty->assign('ordered_adr_fields', $ordered_adr_fields);
}
/**
+72
View File
@@ -0,0 +1,72 @@
/*
* 2007-2013 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2013 PrestaShop SA
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
function validate_isName(s)
{
var reg = /^[^0-9!<>,;?=+()@#"°{}_$%:]+$/;
return reg.test(s);
}
function validate_isGenericName(s)
{
var reg = /^[^<>={}]+$/;
return reg.test(s);
}
function validate_isAddress(s)
{
var reg = /^[^!<>?=+@{}_$%]+$/;
return reg.test(s);
}
function validate_isPostCode(s)
{
var reg = /^[a-z 0-9-]+$/i;
return reg.test(s);
}
function validate_isCityName(s)
{
var reg = /^[^!<>;?=+@#"°{}_$%]+$/;
return reg.test(s);
}
function validate_isMessage(s)
{
var reg = /^[^<>{}]+$/;
return reg.test(s);
}
function validate_isPhoneNumber(s)
{
var reg = /^[+0-9. ()-]+$/;
return reg.test(s);
}
function validate_isDniLite(s)
{
var reg = /^[0-9a-z-.]{1,16}$/i;
return reg.test(s);
}
+22 -13
View File
@@ -100,7 +100,7 @@ $(function(){ldelim}
{if $field_name eq 'company'}
<p class="text">
<label for="company">{l s='Company'}</label>
<input type="text" id="company" name="company" value="{if isset($smarty.post.company)}{$smarty.post.company}{else}{if isset($address->company)}{$address->company|escape:'html'}{/if}{/if}" />
<input type="text" id="company" name="company" value="{if isset($smarty.post.company)}{$smarty.post.company}{else}{if isset($address->company)}{$address->company|escape:'html'}{/if}{/if}" onkeyup="if (validate_{$address_validation.$field_name.validate}($(this).val())) $(this).css('background-color', '#DFF0D8'); else $(this).css('background-color', '#F2DEDE');" />
</p>
{/if}
{if $field_name eq 'vat_number'}
@@ -108,7 +108,7 @@ $(function(){ldelim}
<div id="vat_number">
<p class="text">
<label for="vat_number">{l s='VAT number'}</label>
<input type="text" class="text" name="vat_number" value="{if isset($smarty.post.vat_number)}{$smarty.post.vat_number}{else}{if isset($address->vat_number)}{$address->vat_number|escape:'html'}{/if}{/if}" />
<input type="text" class="text" name="vat_number" value="{if isset($smarty.post.vat_number)}{$smarty.post.vat_number}{else}{if isset($address->vat_number)}{$address->vat_number|escape:'html'}{/if}{/if}" onkeyup="if (validate_{$address_validation.$field_name.validate}($(this).val())) $(this).css('background-color', '#DFF0D8'); else $(this).css('background-color', '#F2DEDE');" />
</p>
</div>
</div>
@@ -116,38 +116,43 @@ $(function(){ldelim}
{if $field_name eq 'firstname'}
<p class="required text">
<label for="firstname">{l s='First name'} <sup>*</sup></label>
<input type="text" name="firstname" id="firstname" value="{if isset($smarty.post.firstname)}{$smarty.post.firstname}{else}{if isset($address->firstname)}{$address->firstname|escape:'html'}{/if}{/if}" />
<input type="text" name="firstname" id="firstname" value="{if isset($smarty.post.firstname)}{$smarty.post.firstname}{else}{if isset($address->firstname)}{$address->firstname|escape:'html'}{/if}{/if}"
onkeyup="if (validate_{$address_validation.$field_name.validate}($(this).val())) $(this).css('background-color', '#DFF0D8'); else $(this).css('background-color', '#F2DEDE');" />
</p>
{/if}
{if $field_name eq 'lastname'}
<p class="required text">
<label for="lastname">{l s='Last name'} <sup>*</sup></label>
<input type="text" id="lastname" name="lastname" value="{if isset($smarty.post.lastname)}{$smarty.post.lastname}{else}{if isset($address->lastname)}{$address->lastname|escape:'html'}{/if}{/if}" />
<input type="text" id="lastname" name="lastname" value="{if isset($smarty.post.lastname)}{$smarty.post.lastname}{else}{if isset($address->lastname)}{$address->lastname|escape:'html'}{/if}{/if}"
onkeyup="if (validate_{$address_validation.$field_name.validate}($(this).val())) $(this).css('background-color', '#DFF0D8'); else $(this).css('background-color', '#F2DEDE');" />
</p>
{/if}
{if $field_name eq 'address1'}
<p class="required text">
<label for="address1">{l s='Address'} <sup>*</sup></label>
<input type="text" id="address1" name="address1" value="{if isset($smarty.post.address1)}{$smarty.post.address1}{else}{if isset($address->address1)}{$address->address1|escape:'html'}{/if}{/if}" />
<input type="text" id="address1" name="address1" value="{if isset($smarty.post.address1)}{$smarty.post.address1}{else}{if isset($address->address1)}{$address->address1|escape:'html'}{/if}{/if}"
onkeyup="if (validate_{$address_validation.$field_name.validate}($(this).val())) $(this).css('background-color', '#DFF0D8'); else $(this).css('background-color', '#F2DEDE');" />
</p>
{/if}
{if $field_name eq 'address2'}
<p class="required text">
<label for="address2">{l s='Address (Line 2)'}</label>
<input type="text" id="address2" name="address2" value="{if isset($smarty.post.address2)}{$smarty.post.address2}{else}{if isset($address->address2)}{$address->address2|escape:'html'}{/if}{/if}" />
<input type="text" id="address2" name="address2" value="{if isset($smarty.post.address2)}{$smarty.post.address2}{else}{if isset($address->address2)}{$address->address2|escape:'html'}{/if}{/if}"
onkeyup="if (validate_{$address_validation.$field_name.validate}($(this).val())) $(this).css('background-color', '#DFF0D8'); else $(this).css('background-color', '#F2DEDE');" />
</p>
{/if}
{if $field_name eq 'postcode'}
{assign var="postCodeExist" value="true"}
<p class="required postcode text">
<label for="postcode">{l s='Zip / Postal Code'} <sup>*</sup></label>
<input type="text" id="postcode" name="postcode" value="{if isset($smarty.post.postcode)}{$smarty.post.postcode}{else}{if isset($address->postcode)}{$address->postcode|escape:'html'}{/if}{/if}" onkeyup="$('#postcode').val($('#postcode').val().toUpperCase());" />
<input type="text" id="postcode" name="postcode" value="{if isset($smarty.post.postcode)}{$smarty.post.postcode}{else}{if isset($address->postcode)}{$address->postcode|escape:'html'}{/if}{/if}" onkeyup="$('#postcode').val($('#postcode').val().toUpperCase()); if (validate_{$address_validation.$field_name.validate}($(this).val())) $(this).css('background-color', '#DFF0D8'); else $(this).css('background-color', '#F2DEDE');" />
</p>
{/if}
{if $field_name eq 'city'}
<p class="required text">
<label for="city">{l s='City'} <sup>*</sup></label>
<input type="text" name="city" id="city" value="{if isset($smarty.post.city)}{$smarty.post.city}{else}{if isset($address->city)}{$address->city|escape:'html'}{/if}{/if}" maxlength="64" />
<input type="text" name="city" id="city" value="{if isset($smarty.post.city)}{$smarty.post.city}{else}{if isset($address->city)}{$address->city|escape:'html'}{/if}{/if}" maxlength="64"
onkeyup="if (validate_{$address_validation.$field_name.validate}($(this).val())) $(this).css('background-color', '#DFF0D8'); else $(this).css('background-color', '#F2DEDE');" />
</p>
{*
if customer hasn't update his layout address, country has to be verified
@@ -199,7 +204,8 @@ $(function(){ldelim}
{if $postCodeExist eq "false"}
<p class="required postcode text hidden">
<label for="postcode">{l s='Zip / Postal Code'} <sup>*</sup></label>
<input type="text" id="postcode" name="postcode" value="{if isset($smarty.post.postcode)}{$smarty.post.postcode}{else}{if isset($address->postcode)}{$address->postcode|escape:'html'}{/if}{/if}" onkeyup="$('#postcode').val($('#postcode').val().toUpperCase());" />
<input type="text" id="postcode" name="postcode" value="{if isset($smarty.post.postcode)}{$smarty.post.postcode}{else}{if isset($address->postcode)}{$address->postcode|escape:'html'}{/if}{/if}" onkeyup="$('#postcode').val($('#postcode').val().toUpperCase());"
onkeyup="if (validate_{$address_validation.$field_name.validate}($(this).val())) $(this).css('background-color', '#DFF0D8'); else $(this).css('background-color', '#F2DEDE');" />
</p>
{/if}
{if $stateExist eq "false"}
@@ -212,22 +218,25 @@ $(function(){ldelim}
{/if}
<p class="textarea">
<label for="other">{l s='Additional information'}</label>
<textarea id="other" name="other" cols="26" rows="3">{if isset($smarty.post.other)}{$smarty.post.other}{else}{if isset($address->other)}{$address->other|escape:'html'}{/if}{/if}</textarea>
<textarea id="other" name="other" cols="26" rows="3"
onkeyup="if (validate_{$address_validation.other.validate}($(this).val())) $(this).css('background-color', '#DFF0D8'); else $(this).css('background-color', '#F2DEDE');">{if isset($smarty.post.other)}{$smarty.post.other}{else}{if isset($address->other)}{$address->other|escape:'html'}{/if}{/if}</textarea>
</p>
{if isset($one_phone_at_least) && $one_phone_at_least}
<p class="inline-infos required">{l s='You must register at least one phone number.'}</p>
{/if}
<p class="text">
<label for="phone">{l s='Home phone'}</label>
<input type="text" id="phone" name="phone" value="{if isset($smarty.post.phone)}{$smarty.post.phone}{else}{if isset($address->phone)}{$address->phone|escape:'html'}{/if}{/if}" />
<input type="text" id="phone" name="phone" value="{if isset($smarty.post.phone)}{$smarty.post.phone}{else}{if isset($address->phone)}{$address->phone|escape:'html'}{/if}{/if}"
onkeyup="if (validate_{$address_validation.phone.validate}($(this).val())) $(this).css('background-color', '#DFF0D8'); else $(this).css('background-color', '#F2DEDE');" />
</p>
<p class="{if isset($one_phone_at_least) && $one_phone_at_least}required {/if}text">
<label for="phone_mobile">{l s='Mobile phone'}{if isset($one_phone_at_least) && $one_phone_at_least} <sup>*</sup>{/if}</label>
<input type="text" id="phone_mobile" name="phone_mobile" value="{if isset($smarty.post.phone_mobile)}{$smarty.post.phone_mobile}{else}{if isset($address->phone_mobile)}{$address->phone_mobile|escape:'html'}{/if}{/if}" />
<input type="text" id="phone_mobile" name="phone_mobile" value="{if isset($smarty.post.phone_mobile)}{$smarty.post.phone_mobile}{else}{if isset($address->phone_mobile)}{$address->phone_mobile|escape:'html'}{/if}{/if}" onkeyup="if (validate_{$address_validation.phone_mobile.validate}($(this).val())) $(this).css('background-color', '#DFF0D8'); else $(this).css('background-color', '#F2DEDE');" />
</p>
<p class="required text" id="adress_alias">
<label for="alias">{l s='Please assign an address title for future reference.'} <sup>*</sup></label>
<input type="text" id="alias" name="alias" value="{if isset($smarty.post.alias)}{$smarty.post.alias}{else if isset($address->alias)}{$address->alias|escape:'html'}{elseif !$select_address}{l s='My address'}{/if}" />
<input type="text" id="alias" name="alias" value="{if isset($smarty.post.alias)}{$smarty.post.alias}{else if isset($address->alias)}{$address->alias|escape:'html'}{elseif !$select_address}{l s='My address'}{/if}"
onkeyup="if (validate_{$address_validation.alias.validate}($(this).val())) $(this).css('background-color', '#DFF0D8'); else $(this).css('background-color', '#F2DEDE');" />
</p>
</fieldset>
<p class="submit2">