Form Validation using JavaScript

后端 未结 5 1228
-上瘾入骨i
-上瘾入骨i 2021-01-25 02:44

I\'m trying to validate a form using JavaScript but i\'m a bit stuck on displaying a message next to the field to say that \"This field is required\". How do I go about doing th

5条回答
  •  暖寄归人
    2021-01-25 03:30

    Add a span near each field so you can use it for the validation message:

    First Name
    
      
      
    
    

    After that, just add the error message there, so instead of:

    alert('You need to complete this field')
    

    You would use:

    document.getElementById('firstname_error').innerHTML = 'You need to complete this field'
    

提交回复
热议问题