-1
Prem440
3y

I have JS code like this to phone num validation... But, it's showing as "valid" msg for the mobile num which i had entered is started with 0,1,2,3,4,5...
But, I want "Valid" msg for mobile num which is starting with 6-9 only.
(Remaing code & functionality are working correctly in/with HTML & CSS code)

Ex : 6012345987 --> valid

JS :
---
function checkPhoneNum()
{
var phone = document.getElementById("pNum").value
var phoneLen = phone.length

var regExp1 = /[0-9]/
var regExp2 = /[^6-9]/
var regExp4 = /[^0-9]/

if (phoneLen == 0)
{
var msg = ""
document.getElementById("phn-err").innerHTML = msg
}

else if (phone.match(regExp1) && phoneLen == 13)
{
var msg = "Valid"
document.getElementById("phn-err").innerHTML = msg
}

else if (phone.match(regExp4) || phone.match(regExp2))
{
var msg = "Invalid"
document.getElementById("phn-err").innerHTML = msg
}
}

Comments
  • 0
    i think your regex is wrong.

    regex 2 and regex 4: /^[6-9]/ instead of /[^6-9]/
    instead of asking whether the first caracter is 6-9 you ask if string contains a caracter ^ or caracters 6-9 wherever in the string.
  • 0
    also, isnt there a way of doing that without regex?
    like

    if (string[0] > '5')
    {
    var msg = "valid"
    }

    (btw, im not a js guy. forcing me to use methods and even regex for such trivialities is what makes me hate oop)
  • 0
    @bad-frog ok, I'll try like this...
  • 0
    @Prem440 to clarify: im pretty sure about the regex, but like i stated, i dont really know JS.

    also changing a caracter in your regexp is faster than rewriting the whole shebang:)

    the latter is just an idea. dont know how JS treats caracters tho. you might have to pass said caracter through parseInt before you can do the comparison
  • 1
    Stackoverflow
  • 0
  • 1
    Did you just post this shit even after being warned not to? Please leave this platform.
  • 1
    Regexp is wrong. And you don't need it in the first place. Use your head instead of random stuff from SO
  • 2
    I just feel bad for the poor regExp3 that disappeared in mysterious circumstances
  • 1
    homie, devRant isn't for getting help with your code problems, that's what StackOverflow is for.
  • 2
    @sudo-woodo even stack overflow will tell him to get the fuck out with such beginner level questions.
  • 0
    @sudo-woodo is there Stack Overflow app is available in playstore...??
  • -1
    @iiii 😆😂🤣
  • 0
    @HCC5GDKc7 All the best 👍🏻
  • 0
    Seriously ? Just use fucking libphonenumber.
Add Comment