methods: {
sendPayment: function(e) {
if(this.to) {
// do your post
}
}
}
A simple if statement should be sufficient enough to check if it's null or not.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
How can I check within the JS that a field isn't empty?
methods: {
sendPayment: function(e) {
// I need to check this isn't empty, before passing a POST request into it.
var to = this.to;
}
}
So that the user has typed something into this field or will the v-model directive take care of that anyhow?
For anyone interested I got this working by doing:
Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('#token').getAttribute('value');
new Vue({
el: '#demo-app',
data: {
to: '',
from: '',
text: '',
sentRequest: false
},
ready:function(){
window.onbeforeunload = this.leaving;
window.onblur = this.leaving;
},
methods: {
leaving:function() {
if (! this.to) {
var p = this.$http.post('/demo/send', { to: this.to, from: this.from, text: this.text });
this.sentRequest = true;
}
return null;
}
}
});
Please or to participate in this conversation.