Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript - Forms - Validating numbers and email
    primarykey
    data
    text
    <p>I am not great with scripting or coding, so I'm really sorry if this is a silly question.</p> <p>I have a form:</p> <pre><code>&lt;form name="userinfo" action="validform.html" method="post" onsubmit="return validateForm()"&gt; &lt;fieldset&gt; &lt;legend&gt;Personal Information&lt;/legend&gt;&lt;br /&gt; &lt;label for="firstname"&gt;First Name:&lt;/label&gt; &lt;input type="text" id="firstname" name="firstname"&gt; &lt;span id="firstname_required"&gt;&lt;/span&gt;&lt;br /&gt; &lt;label for="surname"&gt;Surname:&lt;/label&gt; &lt;input type="text" id="surname" name="surname"&gt; &lt;span id="surname_required"&gt;&lt;/span&gt;&lt;br /&gt; &lt;label for="tel"&gt;Tel No:&lt;/label&gt; &lt;input type="tel" id="tel" name="tel"&gt; &lt;span id="tel_required"&gt;&lt;/span&gt;&lt;br /&gt; &lt;label for="email"&gt;Email:&lt;/label&gt; &lt;input type="email" id="email" name="tel"&gt; &lt;span id="email_required"&gt;&lt;/span&gt;&lt;br /&gt; &lt;label for="password"&gt;Password:&lt;/label&gt; &lt;input type="password" id="password" name="password"&gt; &lt;span id="password_required"&gt;&lt;/span&gt;&lt;br /&gt; &lt;label for="submit"&gt;&lt;/label&gt; &lt;input type="submit" value="Submit!"&gt; &lt;/fieldset&gt; &lt;/form&gt; </code></pre> <p>The form works great. </p> <p>I would like to validate the Tel No, so that only digits between 0-9 can be entered. I'm not worried about length or anything rock n roll like that. I just want to make sure that users enter in numbers, then I can expand on that. Here is my code:</p> <pre><code>function validateNumber() { var a=document.forms["userinfo"]["tel"].value; var patt=new RegExp("[0-9]"); if (a==null || a=="") { document.getElementById('tel_required').innerHTML="required"; } else if (patt==null || patt !== RegExp ) { document.getElementById('tel_required').innerHTML="numbers only allowed"; } } </code></pre> <p>I suspect I have something fundamentally wrong in my else if statement ((patt==null || patt !== RegExp )) or that it's even constructed correctly, or that it should even be an else if statement!! But I can't even seem to get the logic quite right in my head.</p> <p>Any help would be great. Thanks, and again sorry if this is a silly question. </p> <p>Here is the full JavaScript code I have so far:</p> <pre><code>function validateForm() { var a=document.forms["userinfo"]["firstname"].value; if (a==null || a=="") { document.getElementById('firstname_required').innerHTML="required"; } var a=document.forms["userinfo"]["surname"].value; if (a==null || a=="") { document.getElementById('surname_required').innerHTML="required"; } validateNumber() validateEmail() var a=document.forms["userinfo"]["password"].value; if (a==null || a=="") { document.getElementById('password_required').innerHTML="required"; return false; } } function validateNumber() { var a=document.forms["userinfo"]["tel"].value; var patt=new RegExp("[0-9]"); if (a==null || a=="") { document.getElementById('tel_required').innerHTML="required"; } if (patt==null || patt !== RegExp ) { document.getElementById('tel_required').innerHTML="numbers only allowed"; } } function validateEmail() { var a=document.forms["userinfo"]["email"].value; var patt=new RegExp("/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/"); if (a==null || a=="") { document.getElementById('email_required').innerHTML="required"; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload