2

I want users to submit youtube URLs. I want to check "on the fly" if a youtube link is correct and change the text next to the input to OK if the check succeeds.

I managed to make a validation function but it doesn't work. What am I doing wrong?

UPDATE

It still doesnt work, it should say not ok when the URL is incorrect and OK when URL is correct while typing:

            $('form #youtube').bind("change keyup input", validYT());
        function validYT()
        {

            var url = $('form #youtube').val();
            var p = /^(?:https?:\/\/)?(?:www\.)?youtube\.com\/watch\?(?=.*v=((\w|-){11}))(?:\S+)?$/;
            if (url.match(p)) {
                $('#ytInfo').removeClass().addClass('fieldok ').text('OK');
                return true
            }
            else
            {
                $('#ytInfo').removeClass().addClass('fieldok ').text('NOT OK');
                return false
            }
        }

HTML:

youtubelink<BR>
<input type="text" name="youtube" id ="youtube" value="" /><div id="ytlInfo">dd</div>
2

2 Answers 2

11
function validYT(url) {
  var p = /^(?:https?:\/\/)?(?:www\.)?youtube\.com\/watch\?(?=.*v=((\w|-){11}))(?:\S+)?$/;
   return (url.match(p)) ? RegExp.$1 : false;
}

Thanks @eyecatchUp https://stackoverflow.com/a/10315969/1250044

Update

function ytVidId(url) {
    var p = /^(?:https?:\/\/)?(?:www\.)?youtube\.com\/watch\?(?=.*v=((\w|-){11}))(?:\S+)?$/;
    return (url.match(p)) ? RegExp.$1 : false;
}

$('#youtube').bind("change keyup input", function() {
    var url = $(this).val();
    if (ytVidId(url) !== false) {
        $('#ytlInfo').addClass('fieldok');
    } else {
        $('#ytlInfo').removeClass('fieldok');
    }
});

Demo

Sign up to request clarification or add additional context in comments.

4 Comments

I see.. but how to attach it like i do to a input. and give message OK or NOT ok on key event. Basicly i want a input field witch tels you realtime if your link is ok...
Made some adjustments and finaly got it working thanks to your awnser!
@yckart Hey, since your answer was a direct quote of mine (thanks for the mention), I updated the pattern in your answer to match the update to my cited answer. Hope that's fine for you?! :) Cheers
@yckart Seems like my edit wasn't approved (yet).. Maybe you can!?
1

I have created an Method by using the Above method

$(document).ready(function(){

    $.validator.addMethod("youtube", function(value, element) {
     var p = /^(?:https?:\/\/)?(?:www\.)?youtube\.com\/watch\?(?=.*v=((\w|-){11}))(?:\S+)?$/;
     return (value.match(p)) ? RegExp.$1 : false;
    }, "Enter correct URL");

    var validator = $("#frmAddvideo").validate({
        errorElement:'div',
        rules: {
            useravideo: {
                required: true,
                youtube: "#useravideo"
            }

        },
        messages: {
            useravideo: {
                required: "Enter user A video URL",
            }
        }
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.