• Resolved vijalesh

    (@vijalesh)


    hi team is there a way i can get the input box or name text field to accept only characters ? been trying to get the input box to accept only characters but no luck . please guide, it should only take characters [a-zA-Z] no spaces and other special characters

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @vijalesh

    I hope you’re well today!

    Out of the box those fields would accept also digits and special characters but you can use this custom code to restrict them to letters only:

    <?php
    add_filter(
    	'forminator_custom_form_submit_errors',
    	function( $submit_errors, $form_id, $field_data_array ) {
    		$field_name = 'name-1';
    		$submitted_fields = wp_list_pluck( $field_data_array, 'value', 'name' );
    
    		if( !preg_match( '/^[a-z]+$/i', $submitted_fields[ $field_name ] ) ) {
    			$submit_errors[][ $field_name ] = __( 'There is something wrong ' );
    		}
    
    		return $submit_errors;
    	},
    	10, 
    	3
    );

    In this line you would need to replace the “name-1” with the ID of the fiedl you want to check (if different than “name-1”) and then:

    – create an empty file with a .php extension (e.g. “forminator-field-characters-only.php”)

    – paste code into it

    – save the file and upload it to the “/wp-content/mu-plugins” folder of your site’s WordPress installation.

    Best regards,
    Adam

    Thread Starter vijalesh

    (@vijalesh)

    Hi thanks for this .. and if there is more than one field on the same form, i will have to copy this functions that many number of times ? and change the name-1 to name-2 name-3 etc ? right ?

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @vijalesh

    Thanks for response!

    I have modified the code slightly so it should now work with multiple fields. Here is a new version of the code:

    <?php
    add_filter(
    	'forminator_custom_form_submit_errors',
    	function( $submit_errors, $form_id, $field_data_array ) {
    				
    		$fields_names = array( 'name-1', 'name-2' ); // comma separated list of fields
    		
    		$submitted_fields = wp_list_pluck( $field_data_array, 'value', 'name' );
    		
    		foreach ( $fields_names as $key=>$field_name ) {
    
    			if( !preg_match( '/^[a-z]+$/i', $submitted_fields[ $field_name ] ) ) {
    				$submit_errors[][ $field_name ] = __( 'There is something wrong ' );
    			}
    			
    		}		
    		
    
    		return $submit_errors;
    	},
    	10, 
    	3
    );

    You just need to list your fields in this line (following example):

    $fields_names = array( 'name-1', 'name-2' ); // comma separated list of fields

    Best regards,
    Adam

    Thread Starter vijalesh

    (@vijalesh)

    This is good thanks or the solutions

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Have input name field to accept only alaphabets’ is closed to new replies.