C
C
cactuss2014-04-20 20:19:24
JavaScript
cactuss, 2014-04-20 20:19:24

Form validation (required fields)

This is the first time I've come across such a situation.
Here is a similar form.

<form action='name.php'>
Введите ваше имя: <input name='name' type='name' value='namel' />
Введите email: <input name='email' type='email' value='Не email' /> или
Введите сотовый телефон: <input name='tel1' type='tel' pattern='8[0-9]{10}'/>
<input type='submit' value='Отправить' />

It is necessary that 1 of the fields must be filled in "phone" or "mail"
How to implement this? And you can still validate against incorrect data, xss prevention.
Thank you)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2014-04-20
Protko @Fesor

<input name='email' type='email' value='Не email' required='required' />

1
1001001 111, 2014-04-21
@IgorO2

Well, check for an empty value

if(document.getElementsByName('email')[0].value === ""
    && document.getElementsByName('tel1')[0].value === ""
) {
    alert("Пожалуйста заполните, 'email' или 'telephone'");
}

G
GM2mars, 2014-04-21
@GM2mars

on jquery with trimming spaces
if (!$.trim($('input[name=email]').val()).length && !$.trim($('input[name=tel1]').val( )).length) {
console.log("Please fill in 'email' or 'telephone'")
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question