Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sam Boswell 21 posts 106 karma points
    Oct 22, 2014 @ 13:12
    Sam Boswell
    0

    Specified cast is not valid when regex fails

    Hi guys,

    I'm struggling a little to get validation working on a signup form.

    I have an email field that is both mandatory and with some regex to validate that it is a valid email.

    When a user enters a valid email - then the form works fine, however if an invalid email address is entered, then the page does not display the expected little message as it does for mandatory items, instead taking the user to a page that simply reports "Specified cast is not valid".

    Any advice on how to fix this would be much appreciated!

    Screengrab of the email field:

    Email field in Contour

    Regex being used to validate emails:

    (?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
    
  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Oct 22, 2014 @ 13:29
    Dennis Aaen
    0

    Hi Sam,

    To me it looks like that you are starting and ending your regular expression wrong.

    I assme that you found the regex here: http://www.regular-expressions.info/email.html

    Try this instead.

    ^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\]$

    Or try to have a look on this post: http://our.umbraco.org/forum/umbraco-pro/contour/25041-regular-expression-for-valid-email-is-case-sensitive

    Hope this helps,

    /Dennis

  • Sam Boswell 21 posts 106 karma points
    Oct 22, 2014 @ 14:26
    Sam Boswell
    0

    Heh, thank you for that - a good step in the right direction I think. The page shows a different error now of:

    parsing "^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\]$" - Too many )'s.
    

    I tried the regex from the linked post, which returns the old "Specified cast is not valid" error

    It seems strange that when inspecting the page, the form class="field-validation-valid" changes to valid when any data is input, and does not seem to check the regex at all!

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Oct 22, 2014 @ 15:01
    Dennis Aaen
    0

    Hi Sam,

    What if you tried with this regex for validation of the email,

    ^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,6})$

    Did you still get the "Specified cast is not valid" error or?

    Hope this helps,

    /Dennis

  • Sam Boswell 21 posts 106 karma points
    Oct 22, 2014 @ 15:14
    Sam Boswell
    0

    Hi Dennis,

    With that regex, I still see the same error of "Specified cast is not valid"

    I've taken a grab of the source that is being formed by the field when it contains the data "asfd".

    enter image description here

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Oct 22, 2014 @ 15:27
    Dennis Aaen
    0

    Hi Sam,

    I just tried to install the Contour 3.0.21 into a fresh Umbraco install version 7.1.8, and then I created a new form based on the standard pre-defined contact form.

    And for the email field in the pre-defined contact form, it uses this regular expression:

    ^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$

    /Dennis

     

  • Sam Boswell 21 posts 106 karma points
    Oct 22, 2014 @ 16:02
    Sam Boswell
    0

    I'm using Umbraco Contour version 3.0.21 and Umbraco 7.1.8

    At this point - I'm doubting that it's the regex itself that is causing the issue: the field seems not to respond to input that is valid / invalid against regex in any way.

    A regex on a test field of [abc] still will not give me any validation messages :(

  • Sam Boswell 21 posts 106 karma points
    Oct 24, 2014 @ 11:59
    Sam Boswell
    100

    After all that - it turns out to be unconnected from the Regex!

    The following link gave me a little insight: http://our.umbraco.org/forum/umbraco-pro/contour/49251-Specified-cast-is-not-valid?p=0

    The datepicker had had the date format changed to be in the UK format, and so changes were needed to FieldType.DatePicker.cshtml within the contour plugin folder.

    We now parse the date using the following line:

        d = DateTime.ParseExact(Model.Value, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
    

    This removes the invalid cast errors, and the regex validation now triggers on other fields as expected.

    Thanks for your help Dennis (I am using the updated email regex now also)

Please Sign in or register to post replies

Write your reply to:

Draft