Reputation: 13
Good evening. I have created a function for an input field, which the user is asked to select url for his account.
The piece that communicates with the database I have created and works perfectly. Also the piece to make the letters lower.
But I do not know how to district accepting special characters and spaces.
I found some solution in internet but input it call second function. I dont want to change my function. I want somehow to merge in my existing function.
My html is
<input type="text" name="urlcheck" value="" id="urlcheck" class="form-control" placeholder="Write your url. " required onkeyup="urlcheck()"/>
My JS is
function urlcheck(){
var x = document.getElementById("urlcheck");
x.value = x.value.toLowerCase();
var urlcheck = $('#urlcheck').val();
if(urlcheck != '')
{
$.ajax({
url:"<?php echo base_url(); ?>portolio/check_url",
method:"POST",
data:{url:url},
success:function(data){
data.trim();
if(data == '1') {
$('#url_result').html('<label class="text-danger">Url is not free</label>');
} else {
$('#url_result').html('<label class="text-success">Url is free.</label>');
}
}
});
}
};
The solution i find but i dont know how merge is this:
<script type="text/javascript">
function blockSpecialChar(e){
var k;
document.all ? k = e.keyCode : k = e.which;
return ((k > 96 && k < 123) || k == 8 || (k >= 48 && k <= 57));
}
</script>
</head>
<body>
<form id="frm" runat="server">
<input type="text" name="folderName" onkeypress="return blockSpecialChar(event)"/>
</form>
So... I thing i need help how "insert" the bellow code in my ajax function
var k;
document.all ? k = e.keyCode : k = e.which;
return ((k > 96 && k < 123) || k == 8 || (k >= 48 && k <= 57));
EDIT: In my input field i dont need from user to write the whole url. Only the last part. For example if a user want to have example.com/myname. The user must fill the myname. That i want to check!
Upvotes: 1
Views: 5824
Reputation: 47904
You don't need to program javascript for this task. Just use the pattern
attribute.
Try my runnable snippet. Type in a non-alphanumeric character, then click Submit. The submission will be halted.
<form>
<input type="text" placeholder="example.com/[your input]" pattern="[a-zA-Z0-9]+">
<button type="submit">Submit</button>
</form>
Upvotes: 1
Reputation: 13
Use this pattern i found. It only accepts urls.
<input type="text" name="urlcheck" value="" id="urlcheck" class="form-control"
placeholder="Write your url. " required onkeyup="urlcheck()" required="" data-bv-
regexp="true" data-bv-regexp-regexp="https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9]\.[^\s]{2,}"
data-bv-regexp-message="Invalid Url">
Upvotes: 0