I'm using Contactform 7 for Wordpress. I've edited the text.php and textarea.php to enable the use of placeholders. That works fine.
But, the validation isn't working for some input fields, 'cause the validation thinks the placeholder is the actual text being validated. And yes, ofcourse that validates true 'cause it's not empty.
My input fields with placeholders are as follows:
Your name
Your e-mailadress
Your telephone number
and so on.
In contactform 7's scripts.js I want the validation to check if the input isn't the same as the placeholder. So if the input value is the same as the placeholder value, do the CF7's error validation.
Any idea?
In scripts.js of the contactform 7 plugin folder
Change this:
var submit = form.find('input:submit');
if (! submit.length) return;
with this:
var submit = form.find('input:submit');
if (! submit.length || submit == this.find('[placeholder]').wpcf7Placeholder()) return;
Related
enter image description here I am using ngxdatatable in reactive forms where user can update all the fields in the table. i have three fields/columns for e.g fields a,b and c and they are dynamic as created below:
-- code for ngxdatatable columns
<div [formgroup]= this.form[rowindex]
<ngx-datatable-column *ngFor="let column of columns; let i = index;" name="{{column.name}}" prop="{{column.prop}}">
<ng-template #buttonsTemplate let-row="row" let-value="value" ngx-datatable-cell-template>
<input type =text isReadonly ="isreadonly">
</ng-template>
</ngx-datatable-column></div>
I am facing challenge in defining my validation for field B/column B,
1. **field b** can be autocalculated as number from field a and field c , **b = a+c**, but
user can't enter number manually in field b , on manual input only few strings are allowed like
'test', 'ok', 'any'. how can I achieve this ?
2. I tried to implement it via custom validation which works fine during manual input but it is failing because field/col B is autocalculated it shows error for number which is a valid scenario , as validation is only to be shown when user tries to input manually.
> this.fb.builder{ field a = validtion.Required,
> validation.patter(_number) field c=validtion.Required,
> validation.patter(_number) field b = customValidator }
>
>
> export function customValidator(c:abstract control) :{key:value}{
> if(c.value=='test' || c.value==''any || c.value == 'ok'} return null
>
> else {allowed:false} }
3. I thought of trying to achieve it with keydown event using directive , but not sure how to write a directive which allows words 'any', 'ok', 'test' and not anything else.**please suggest how can i write the directive for this as I am new to angular.**
4. Please suggest if you think any better approach can be used.
I wrote a function to update Validator rules on an input if a certain option was selected, using this method (the forms are built using FormGroup):
onValueChanged(data : any) {
let appVIP1 = this.vip1TabForm.get('option1');
let appVIP2 = this.vip2TabForm.get('option2');
let appVIP3 = this.vip3TabForm.get('option3');
//Set required validation if data is 'option3'
if(data != 'option3') {
//Due to initialization errors in UI, need to start with the case
//That there are validations, check to remove them
appVIP1.setValidators([]);
appVIP2.setValidators([]);
appVIP3.setValidators([]);
}
else {
appVIP1.setValidators([Validators.required]);
appVIP2.setValidators([Validators.required]);
appVIP3.setValidators([Validators.required]);
}
}
And I bind that function call to a click event on radio buttons (I initially used the guide from this answer, but the onChange function didn't bind correctly).
This works great, and if the user selects option 1 or 2, the validations are empty, and won't be triggered. If they select option 3, the validations are shown and submission is stopped. However, I run into the problem where the user submits, sees the error, and goes back to change to option 1 or 2. While the validator is cleared, my form still reads as invalid. I have multiple input fields I am validating, so I can't just set the form to valid if the validator is removed this way. How would I go about doing this? Can I remove the has-error for one particular field in the formgroup?
If the correct validators are in place, you can manually call AbstractControl#updateValueAndValidity after they select an option:
this.formBuilder.updateValueAndValidity();
(Where, of course, this.formBuilder is your FormBuilder instance.)
You can also call it on FormElements directly.
This is commonly used to trigger validation after a form element's value has been programmatically changed.
Instead of removing and adding validations. It is more simple to enable and disable fields. You need to add the Validators.required for all required fields. And disable the fields which are not required.
onValueChanged(data : any) {
let appVIP1 = this.vip1TabForm.get('option1');
let appVIP2 = this.vip2TabForm.get('option2');
let appVIP3 = this.vip3TabForm.get('option3');
if(data != 'option3') {
appVIP1.disable();
appVIP2.disable();
appVIP3.disable();
}
else {
appVIP1.enable();
appVIP2.enable();
appVIP3.enable();
}
}
I'm attempting to preprocess CSS and placeholder etc to the form inputs for the Registration Form. I'm manually rendering each field input on the registration form.
The following code works fine for email or password, but not 'custom fields'.
$form['account']['mail']['#attributes']['placeholder'] = t('Email address');
$form['account']['mail']['#attributes']['class'][] = 'email-input';
$form['account']['mail']['#title_display'] = "invisible";
$form['account']['mail']['#description'] = t('');
When I apply the same to the custom fields, it won't apply.
I have looked through the user module and printed avialable varibles
$form['field_first_name']['#attributes']['placeholder'] = t('First Name');
$form['field_first_name']['#attributes']['class'][] = 'first-name-input';
$form['field_first_name']['#title_display'] = "invisible";
$form['field_first_name']['#description'] = t('');
I found multiple mentions of field_first_name in the variable print out, but I'm unable to modify the text inputs directly.
I have a form which has both server side and client side validation.
It is an edit form, so the original user values are originally pre-populated.
e.g. The original pre-populated values are:
username = yeo
surname = yang
phonenumber = 11-12345
Now, the user edits to the below and submits.
e.g. The edited submitted values are:
username = yeoNew
surname = yangNew
phonenumber = 12-1111
This gets submitted to the serverside and fails the serverside validation because the phonenumber starting with 12 is not allowed.
Anyway, so the form is displayed back to the user as
e.g. The redisplayed form values are:
username = yeoNew
surname = yangNew
phonenumber = 12-1111
This is because my form allows the user to remember their submitted values.
At this stage, I'd like to allow the user to have the ability to reset the form values to the original values using clientside javascript. This is like a reset feature.
e.g. The reset button will restore the form values to:
username = yeo
surname = yang
phonenumber = 11-12345
The reason for this reset feature is that I want the user to have the option to edit the phonenumber again from the original values.
My question is:
What is a good way to keep track of the original values within the HTML so that I can restore it with javascript?
I'm thinking a new attribute called orig='' within the form elements which will store this value.
Is that a good idea?
Any other approaches?
thanks
I would use the HTML5 local storage.
See http://www.w3schools.com/html/html5_webstorage.asp
Using jquery I would do it this way:
<script type="text/javascript">
function load() {
if (localStorage["username"]) {
$('#username').val(localStorage["username"]);
}
if (localStorage["surname"]) {
$('#surname').val(localStorage["surname"]);
}
if (localStorage["phone"]) {
$('#phone').val(localStorage["phone"]);
}
}
function save() {
localStorage["username"] = $('#username ').val();
localStorage["surname"] = $('#surname').val();
localStorage["phone"] = $('#phone').val();
}
</script>
I have created a form which gets user data about the city he leaves. The form stores data in a table called profile. I am using a function which inserts data into profile table. If there are already data, the function just updates the table. If the user does not insert data into city field and press submit, the table continues to keep the previous data, because nothing submitted.
In the form I would like to make a validation so that the user will inserts only a-z or space characters.
I have used the following code for validation:
if( empty($_POST) === false ){
if( empty($errors) === true ){
if( preg_match('/^[a-zA-Z ]+$/', $_POST['city']) ===0 ){
$errors[]='<font color="#963">City must only contains a-z and spaces</font>';
}
}
}
It works! But the problem now is that if the user leave the form empty and press submit the above code enforces him to place data( says: City must only contains a-z and spaces). How can I use the above code to validate the form only if the user enters data. And if not then to continue without any problem.
You can do this without even generating an error. Replace
if( preg_match('/^[a-zA-Z ]+$/', $_POST['city']) ===0 ){
With
$_POST['city'] = preg_replace("&[^a-zA-Z ]&","",$_POST['city']);
This will replace all non-lowercase letters and non-spaces with nothing before it's inserted into the database.
EDIT:
If you'd still like to generate an error, use
if( preg_match("&[^a-zA-Z ]&",$_POST['city']) ){
$errors[]='<font color="#963">City must only contains a-z and spaces</font>';
}
before the preg_replace.