Grow your CSS skills. Land your dream job.

Javacript URL validation

  • # October 28, 2011 at 4:34 am

    Hi mi currently have the following code that checks if there is the “http://” in front of it and if there is none it adds it

    $('.urlinput').keypress(function(e) {
    if (e.keyCode == 13) {

    var value = $(this).val()
    var ini = $(this).val().substring(0, 4);

    if (ini !== 'http') {
    cur_val = value
    value.val('http://' + cur_val + '/');
    }

    }
    });

    My question is how do i integrate a validation system?

    # October 28, 2011 at 6:24 am

    Try this:

    $('.urlinput').keypress(function(e) {
    // use e.which here, it's been normalized across browsers by jQuery
    if (e.which === 13) {

    var value = $(this).val();
    var ini = $(this).val().substring(0, 4);

    if (ini !== 'http') {
    value = 'http://' + value + '/';
    $(this).val(value);
    }
    // *** ADD your URL validation here; check "value" ***
    }
    });
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".