www.webdeveloper.com
Results 1 to 6 of 6

Thread: form validation without javascript

  1. #1
    Join Date
    Feb 2011
    Location
    London
    Posts
    62

    form validation without javascript

    HI there,
    I wonder if anybody could help me with some form validation issues. Basically, on my website I have a comment form available and the validation is done through javascript.
    Now, assume that a user disables javascript for whatever reason, how can ensure that the form is still validated? Is there anything like CSS validation or HTML validator, or something else?
    The form is available on this page should anybody want to see it:
    http://www.antobbo.webspace.virginme..._and_other.htm
    thanks

  2. #2
    Join Date
    Apr 2011
    Posts
    5
    you can also use server-side scripting to validate the form (like php for example). The draw back of server-side script is that in order to alert the user of errors in the form you would have to refresh the page each time you check the form (which is slower and can get annoying).

    I don't know of anyway to use HTML or CSS, as these are basically static instead of dynamic, meaning that they ARE the webpage rather than code that manipulates the webpage. Hope that helps.

  3. #3
    Join Date
    Feb 2011
    Location
    London
    Posts
    62
    thanks licksick.
    If PHP could get that annoying, I am really not sure what to do then. I wonder how people do validate their forms in the event of javascript being turned off, if they don't use javascript. Do they simply don't care?
    Are you by any chance aware of any website having the PHP validation for forms, I would like to see how that works and how annoying it is : - )

  4. #4
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Every form should be validated server-side, neglecting to do this could get your server hacked and used to send spam.

  5. #5
    Join Date
    Mar 2013
    Location
    Hastings, Hawkes Bay, New Zealand
    Posts
    1
    I've create a small plugin that you can use either as a plugin or theme function that validates custom post type fields without javascript

    add_action('save_post', 'album_save_post', 10, 2);

    function album_save_post( $album_id, $album ) {

    if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || $album->post_type != 'music_album') return;



    // echo '<pre>';
    // print_r($album);
    // echo '</pre>';
    // die();

    $errors = array();

    // Validation filters
    $title = $album->post_title;
    if ( ! $title ) {
    $errors['title'] = "The title is required";
    }

    // if we have errors lets setup some messages
    if (! empty($errors)) {

    // we must remove this action or it will loop for ever
    remove_action('save_post', 'album_save_post');

    // save the errors as option
    update_option('album_errors', $errors);

    // Change post from published to draft
    $album->post_status = 'draft';

    // update the post
    wp_update_post( $album );

    // we must add back this action
    add_action('save_post', 'album_save_post');

    // admin_notice is create by a $_GET['message'] with a number that wordpress uses to
    // display the admin message so we will add a filter for replacing default admin message with a redirect
    add_filter( 'redirect_post_location', 'album_post_redirect_filter' );
    }
    }

    function album_post_redirect_filter( $location ) {
    // remove $_GET['message']
    $location = remove_query_arg( 'message', $location );

    // add our new query sting
    $location = add_query_arg( 'album', 'error', $location );

    // return the location query string
    return $location;
    }

    // Add new admin message
    add_action( 'admin_notices', 'album_post_error_admin_message' );

    function album_post_error_admin_message() {
    if ( isset( $_GET['album'] ) && $_GET['album'] == 'error' ) {
    // lets get the errors from the option album_errors
    $errors = get_option('album_errors');

    // now delete the option album errors
    delete_option('album_errors');

    $display = '<div id="notice" class="error"><ul>';

    // Because we are storing as an array we should loop through them
    foreach ( $errors as $error ) {
    $display .= '<li>' . $error . '</li>';
    }

    $display .= '</ul></div>';

    // finally echo out our display
    echo $display;

    // add some jQuery
    ?>
    <script>
    jQuery(function($) {
    $("#title").css({"border": "1px solid red"})
    });
    </script>
    <?php
    }
    }
    The full tutorial is here:
    http://kevinphillips.co.nz/news/cust...ut-javascript/

  6. #6
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    you can use HTML5. it has required, pattern, and type attributes to validate most common data input and works without javascript on modern browsers.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles