Skip Navigation

[Resolved] How to make User Registration form validate First Name and Last Name fields?

This support ticket is created 7 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 5 replies, has 3 voices.

Last updated by vimalS 7 years, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#438252
Screen Shot 2016-09-20 at 4.04.36 PM.png

I am trying to: On User Registration Form (CRED Create User Form), make First Name and Last Name required with front end validation on submit, similar to Username, Email fields.

#438417

Dear darry,

In the front-end, you can add attribute "required = 'required'" in to both the first_name field and the last_name field, for example, edit your CRED form, in the JS editor window, add below codes:

jQuery( document ).ready(function(){
jQuery( "input[name='first_name']").attr('required', 'required');
jQuery( "input[name='last_name']").attr('required', 'required');
});

In the server side, please follow our document to setup a PHP funtion to validate those field:
https://toolset.com/documentation/user-guides/cred-api/#cfv
Validation Hooks
cred_form_validate: (filter)
This hook provides custom validation for form fields.

#438629
Screen Shot 2016-09-21 at 11.09.23 AM.png

Is there a way to generate the same label element with class="wpt-form-error" as the Username field and Email field, if I click "Submit" with the First Name field and Last Name field blank? See screen shot.

#438760

Yes, it is possible with CRED filter hook "cred_filter_field_before_add_to_form", for example you can add below codes into your theme/functions.php:

add_filter('cred_filter_field_before_add_to_form', 'required_fields_func', 10, 2);
function required_fields_func($field, $computed_values){
	if(in_array($field['id'], array('first_name', 'last_name'))){
		$field['data']['validate']['required'] = array ( 
			'active' => 1, 
			'value' => 1, 
			'message' => 'This field is required' 
		) ;
	}
	return $field;
}
#439034

Thank you so much! That is exactly the solution I was looking for.

#1248969
Screenshot_2.png

In php 7.1 it is giving error.

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.