url validation

This commit is contained in:
hay-kot 2021-02-20 13:39:04 -09:00
commit 9e91f05df8

View file

@ -7,10 +7,13 @@
</v-card-title> </v-card-title>
<v-card-text> <v-card-text>
<v-form> <v-form ref="urlForm">
<v-text-field <v-text-field
v-model="recipeURL" v-model="recipeURL"
:label="$t('new-recipe.recipe-url')" :label="$t('new-recipe.recipe-url')"
required
validate-on-blur
:rules="[isValidWebUrl]"
></v-text-field> ></v-text-field>
</v-form> </v-form>
@ -64,18 +67,20 @@ export default {
methods: { methods: {
async createRecipe() { async createRecipe() {
this.processing = true; if (this.$refs.urlForm.validate()) {
let response = await api.recipes.createByURL(this.recipeURL); this.processing = true;
if (response.status !== 201) { let response = await api.recipes.createByURL(this.recipeURL);
this.error = true; if (response.status !== 201) {
this.processing = false; this.error = true;
return; this.processing = false;
} return;
}
this.addRecipe = false; this.addRecipe = false;
this.processing = false; this.processing = false;
this.recipeURL = ""; this.recipeURL = "";
this.$router.push(`/recipe/${response.data}`); this.$router.push(`/recipe/${response.data}`);
}
}, },
navCreate() { navCreate() {
@ -89,6 +94,10 @@ export default {
this.recipeURL = ""; this.recipeURL = "";
this.processing = false; this.processing = false;
}, },
isValidWebUrl(url) {
let regEx = /^https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)$/gm;
return regEx.test(url) ? true : "Must be a Valid URL";
},
}, },
}; };
</script> </script>