Post by Deliveren » Mon Apr 09, 2012 8:32 am

Hi everyone,

Is it possible to make a field accept numeric and text input? I am trying to make the address 2 field in the checkout to accept :

- Numbers
- Text
- Combination of number and text

And is it possible to change the minimum required input from 3 to 1?

I already looked in this file:/catalog/view/theme/default/template/checkout/address.tlp

And I guess I have to make some changes in the following code:

<td><span class="required">*</span> <?php echo $entry_address_1; ?></td>
<td><input type="text" name="address_1" value="" class="large-field" /></td>

I hope somebody can help me!

Derya

Newbie

Posts

Joined
Sat Feb 04, 2012 8:26 am

Post by dirtboy » Mon Apr 09, 2012 10:03 am

address 2 field is optional, i just tried entering letters and numbers in the field and it allowed me to. address 2 is usually for apartment numbers so letters and numbers are allowed by default.

“Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.”
- Henry Ford


User avatar
Active Member

Posts

Joined
Wed Mar 07, 2012 3:04 pm

Post by Avvici » Mon Apr 09, 2012 10:11 am

If you want to make the field force number input PRIOR to validation then you would need to use simple javascript. If you want the validation script to take care of it then you will set up an is_numeric check in the validate script.

Here is a script of mine that only allows people to type in numbers into an input field.
Open up your catalog/view/javascript/common.js and place this script at the very bottom of the page below everything after the last }

Code: Select all

function isNumberKey(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57)) {
		return false;
	} else {
		return true;
	}
}
Now locate the input field or fields you want this rule to apply to and make it look like this:

Code: Select all

<input type="text" name="telephone" onkeypress="return isNumberKey(event)" value="<?php echo $telephone; ?>" size="32"/>
That input field is the telephone field for customer account registration. Adding this

Code: Select all

onkeypress="return isNumberKey(event)"
to any input field will force only numbers.

Here is another function of mine that I use to validate a field for Dollar Amounts. It only accepts numbers and a decimal point. Use it the same way.

Code: Select all

function isNumberKeyWithDecimal(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46) {
		return false;
	} else {
		return true;
	}
}

User avatar
Expert Member

Posts

Joined
Tue Apr 05, 2011 12:09 pm
Location - Asheville, NC

Post by Deliveren » Mon Apr 09, 2012 7:29 pm

Hi!

First of all thanks for the help.

I just started to work with OpenCart and dont have knowledge of java script. I did how ever solved my problem in a different way yesterday (there were no reply’s on my post at that time). I solved the problem by opening the following file: /public_html/catalog/controller/checkout/guest.php

Here I edited the following lines:

if ((utf8_strlen($this->request->post['address_2']) < 3) || (utf8_strlen($this->request->post['address_2']) > 128)) {
$json['error']['address_2'] = $this->language->get('error_address_2');

in

if ((utf8_strlen($this->request->post['address_2']) < 1) || (utf8_strlen($this->request->post['address_2']) > 128)) {
$json['error']['address_2'] = $this->language->get('error_address_2');

So the only thing I did was changing the 3 in 1.

This also worked. But I am very curious what is the difference between the way I solved the problem and the way you described avvici?

Derya

Newbie

Posts

Joined
Sat Feb 04, 2012 8:26 am

Post by Avvici » Mon Apr 09, 2012 11:01 pm

The difference is that you are editing the Validation code in the Control file. That code takes the form HTTP POST variables and validates them to see if they are correct. If it's not correct then it sends an error back for which is displayed on the form saying "ERROR PLEASE USE ONLY NUMBERS bla bla bla...

There is nothing wrong with that method as stated above but it's easier to just force a proper validation with simple javascript on the front end even before the form variables are sent. It's all up to you :) There are functions in both PHP and Javascript to take care of your validation.

User avatar
Expert Member

Posts

Joined
Tue Apr 05, 2011 12:09 pm
Location - Asheville, NC
Who is online

Users browsing this forum: Majestic-12 [Bot] and 126 guests