0

i'm working on a form where you can put your logo as url. I know how to validate a file extension but not a string from a form. i've started with the code i've mentioned here but im totally lost.

<script>
$('#editForm_Submit').click(function (e) {
    e.preventDefault();
    var re = /^[A-Za-z]+$/;
    if(re.test(document.getElementById("LogoUrl").value))
        alert('Valid Name.');
    else
        alert('Invalid Name.');
});
</script>

It has to preventdefault and check the extention on jpg,png and jpeg. if validate than submit else return false.

I've also tried

<script>
jQuery(document).ready(function () {
    $('#editForm_Submit').click(function (e) {
        e.preventDefault();
        jQuery("input[type=url]").each(function () {
            jQuery(this).rules("add", {
                accept: "png|jpe?g",
                messages: {
                    accept: "Only jpeg, jpg or png images"
                }
            });
        });
    });
});
</script>

I know how to do it with php but i want to try it with javascript or jquery

  • And do i also use that for example They can input a url image like http://www.example.com/image.gif and after submitting that it needs to return false on submit because it has to be png,jpg or jpeg – Brandon Khoury Sep 03 '15 at 14:54

1 Answers1

0

You need a regex of URL:

Check this question Trying to Validate URL Using JavaScript

$('#editForm_Submit').click(function (e) {
    e.preventDefault();
    var re = /^[A-Za-z]+$/;
    if(validateURL(document.getElementById("LogoUrl").value)))
        alert('Valid Name.');
    else
        alert('Invalid Name.');
});


function validateURL(textval) {
  var urlregex = new RegExp(
    "^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&amp;%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&amp;%\$#\=~_\-]+))*$");
  return urlregex.test(textval);
}
Community
  • 1
  • 1
Mosh Feu
  • 28,354
  • 16
  • 88
  • 135