WordPress.org

Ready to get started?Download WordPress

Forums

input field to accept only numbers (1 post)

  1. tiedemies
    Member
    Posted 3 years ago #

    Any help to my problem would be highly appreciated.

    I have a site where visitors can post ads, but the problem is that the field for ad's price should only accept numbers and dots (as in 10.00/100.00, etc.), if something else is typed (or pasted) it should give an error like it does for empty fields that are working like tehy should.

    Here's the "price" input field's code:

    <p><label for="price"><?php _e('Price (only numbers please.)', 'jr'); ?> <span title="required">*</span></label> <input type="text" class="text" name="payment" id="payment" value="<?php if (isset($posted['price'])) echo $posted['price']; ?>" /></p>

    Here's the function for the whole form:

    <?php
    
    function jr_process_submit__form() {
    
    	global $post, $posted;
    
    	$errors = new WP_Error();
    	if (isset($_POST['form_submit']) && $_POST['form_submit']) :
    
    		// Get (and clean) data
    		$fields = array(
    			'your_name',
    			'email',
    			'website',
    			'country',
    			'city',
    			'address',
    			'geo_latitude',
    			'geo_longitude',
    			'details',
    			'price'
    		);
    		foreach ($fields as $field) {
    			if (isset($_POST[$field])) $posted[$field] = stripslashes(trim($_POST[$field]));
    		}
    
    		### Strip html
    
    		if (get_option('jr_html_allowed')=='no') :
    
    			$posted['details'] = strip_tags($posted['details']);
    
    		endif;
    
    		### End strip
    
    		$posted['featureit'] = 'no';
    
    		// Check required fields
    		$required = array(
    			'your_name' => __('Your name', 'jr'),
    			'email' => __('Job title', 'jr'),
    			'job_country' => __('Country', 'jr'),
    			'details' => __('Job description', 'jr'),
    			'price' => __('Payment', 'jr'),
    		);
    
    		if (get_option('submit_cat_required')=='yes') :
    			$submit_cat = array('ad_term_cat' => __('Ad category', 'jr'));
    			$required = array_merge($required, $submit_cat);
    		endif;
    
    		foreach ($required as $field=>$name) {
    			if (empty($posted[$field])) {
    				$errors->add('submit_error', __('<strong>ERROR</strong>: &ldquo;').$name.__('&rdquo; is a required field.', 'jr'));
    			}
    		}
    
    		if ($errors && sizeof($errors)>0 && $errors->get_error_code()) {} else {
    
    			if(isset($_FILES['ad-image']) && !empty($_FILES['ad-logo']['name'])) {
    
    				$posted['ad-image-name'] = $_FILES['ad-logo']['name'];
    
    				// Check valid extension
    				$allowed = array(
    					'png',
    					'gif',
    					'jpg',
    					'jpeg'
    				);
    
    				$extension = strtolower(pathinfo($_FILES['ad-logo']['name'], PATHINFO_EXTENSION));
    
    				if (!in_array($extension, $allowed)) {
    					$errors->add('submit_error', __('<strong>ERROR</strong>: Only jpg, gif, and png images are allowed.'));
    				} else {
    
    					/** WordPress Administration File API */
    					include_once(ABSPATH . 'wp-admin/includes/file.php');
    					/** WordPress Media Administration API */
    					include_once(ABSPATH . 'wp-admin/includes/media.php');
    
    					function company_logo_upload_dir( $pathdata ) {
    						$subdir = '/company_logos'.$pathdata['subdir'];
    					 	$pathdata['path'] = str_replace($pathdata['subdir'], $subdir, $pathdata['path']);
    					 	$pathdata['url'] = str_replace($pathdata['subdir'], $subdir, $pathdata['url']);
    						$pathdata['subdir'] = str_replace($pathdata['subdir'], $subdir, $pathdata['subdir']);
    						return $pathdata;
    					}
    
    					add_filter('upload_dir', 'ad_image_upload_dir');
    
    					$time = current_time('mysql');
    					$overrides = array('test_form'=>false);
    
    					$file = wp_handle_upload($_FILES['ad-image'], $overrides, $time);
    
    					remove_filter('upload_dir', 'ad_image_upload_dir');
    
    					if ( !isset($file['error']) ) {
    						$posted['ad-image'] = $file['url'];
    						$posted['ad-image'] = $file['type'];
    						$posted['ad-image'] = $file['file'];
    					}
    					else {
    						$errors->add('submit_error', __('<strong>ERROR</strong>: ', 'jr').$file['error'].'');
    					}
    
    				}
    			}
    		}		
    
    	endif;
    
    	$submit_form_results = array(
    		'errors' => $errors,
    		'posted' => $posted
    	);
    
    	return $submit_form_results;

    Thank you in advance if anyone has any ideas, I've been stuck with this for days now.

Topic Closed

This topic has been closed to new replies.

About this Topic