• Resolved thehawkman

    (@thehawkman)


    Following the instructions here https://github.com/WebDevStudios/CMB2/wiki/Tips-&-Tricks#limit-text-field-to-numbers-only

    I’ve set a text field to numeric, but when testing, I typed in letters instead of numbers and they were allowed through.

    $cmb->add_field( array(
    ‘name’ => __( ‘price for 1 license’, wds-post-submit ),
    ‘id’ => $prefix . ‘price_for_1_license’,
    ‘type’ => ‘text’,
    ‘type’ => ‘number’,
    ‘attributes’ => array(
    ‘type’ => ‘number’,
    ‘pattern’ => ‘\d*’,
    ),
    ) );

    And here is the result: price_for_1_license is “aaaaaaa”, even though it should not allow letters. Unless it works with hex numbers? ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Seems to be working just fine for me, at least in my dev install. When I try to publish or save, it gives me a little popup warning about only numbers in the field.

    Some quick notes:
    1. you have “type” specified twice, which may or may not be contributing.
    2. Check your dev tools console error to see if any javascript errors are showing up that may be preventing the browser’s validation of the input.

    Thread Starter thehawkman

    (@thehawkman)

    Thousands of users of CMB2 out there and it just happens to me. Sigh… OK. I’ve stripped down the form and even disabled the custom post type plugin I made in case something in it was interfering with CMB2. I’ve posted the simplified form here
    https://pastebin.com/cevet5ts

    Reset the database, tried it with just CMB2 and this https://github.com/CMB2/CMB2-Snippet-Library/blob/master/front-end/cmb2-metabox-shortcode.php# active and with regular posts, although I don’t think it was the custom post type plugin being the problem or else it would be working. I’ve tried to submit letters instead of numbers and what I found is that this time the field just gets ignored (not saved at all) if I insert incorrect (non-numeric) values. As evidenced by enabling custom fields display in the backend of wordpress, the developer name field gets saved, but the numeric only one (where I entered “KKKKK”) isn’t in the database, which I confirmed by looking at the DB in phpmyadmin. WP is 4.7.3, PHP 5.2.8 on XAMPP.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    What browser/browser version are you using?

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    My first test above was assuming it was from the admin, however, with the full code above, I’m seeing the same behavior from the frontend with TwentySeventeen active:

    https://cloudup.com/cb7hptDCcIt

    Edit: the image above is clickable for a larger version.

    Try to type in text and non numerals, and a popup shows up.

    Thread Starter thehawkman

    (@thehawkman)

    I guess you’re not using IE. I’m using IE11 on Windows 8.1 (not always, but sometimes when I’m lazy I will open it up instead of Firefox). In IE the field looks different, no up/down arrows on the right and behaves differently like I said prior. In Chrome it’s different as well, now that we’re on the subject. Firefox and Chrome both displays those arrows on the right, unlike IE, but while Firefox will allow you to type in letters but will popup the warning if you do, Chrome won’t allow you to type in letters at all, you will only be able to type in numbers. I think this issue could use a fix, because that form will unavoidably be used by some people using IE or Edge (which I don’t have). Are you using Javascript validation? Because I can’t help but wonder what will happen if someone disables Javascript in their browser. Does this mean the validation would stop working completely? And in another head scratching moment, it seems this form now refuses to display the “successful submission” message.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    As far as I’m aware, it’s using the browser’s built in validation, no specialized library.

    That said, yes, I’m on Firefox Developer Edition and also OSX, which would explain the differences going on here.

    Still investigating some further thoughts, but wanted to get this reply out there to not leave you in limbo.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Did some internal discussion, and CMB2 doesn’t have any polyfill or validation libraries included, so this area of browser support is going to end up falling upon the user in question, to provide fallback for. If I recall right, browsers that don’t support a number, pun intended, of the new HTML5 input types, fall back to text fields, which explains a lot of details for this specific issue.

    Possible resource to use to help out here would be https://github.com/jonstipe/number-polyfill

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Numeric only text field lets letters through’ is closed to new replies.